LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 October 19 2012

Joe
Member

[Exercise] Letter Diamonds

Here's a cute little exercise that shouldn't take too long to solve:

Given a capital letter (A .. Z) print a diamond starting with 'A' with the supplied letter at the widest point.

For example:

>>> print diamond ('E')
    A
   B B
  C   C
 D     D
E       E
 D     D
  C   C
   B B
    A

>>> print diamond ('C')

  A
 B B
C   C
 B B
  A

Offline

#2 October 19 2012

geek
Member

Re: [Exercise] Letter Diamonds

Diamond[c_] := Block[{},
   n = First[ToCharacterCode[c] - ToCharacterCode["A"]] + 1;
   P = ConstantArray[0, {2 n - 1, 2 n - 1}];
   Do[P[[{i, 2 n - i}, {n - i + 1, n + i - 1}]] = i, {i, n}];
   P = (If[# == 0, " ", FromCharacterCode[# + First@ToCharacterCode["A"] - 1]] & /@ #) & /@ P;
   Print[P // MatrixForm];
];

Offline

#3 October 19 2012

Joe
Member

Re: [Exercise] Letter Diamonds

Answer in CoffeeScript:

diamond_line = (c) ->
        if c == "A" then c
        else
                numvalue = c.charCodeAt(0) - "A".charCodeAt(0) # 65
                "#{c}#{Array(2*(numvalue-1) + 2).join " "}#{c}"

diamond = (c) ->
        numvalue = c.charCodeAt(0) - "A".charCodeAt(0) # 65
        a = (diamond_line String.fromCharCode x+65 for x in [0..numvalue])
        b = (Array(numvalue-y+1).join(" ").concat x for x,y in a)
        c = (a[i] for i in [a.length-2..0])
        d = a.concat b

        return d.join "\n"
        

The same answer in Google's Dart:

String diamond_line(String c) {
    if (c == "A") {
        return c;
    }

    var res = c;
    num numvalue = c.charCodeAt(0) - "A".charCodeAt(0);

    for (var i=0; i <=(2*(numvalue-1)); ++i) {
        res = "$res ";
    }

    return "$res$c";
    
}

String diam_line(var x) {
    var a = "";
    for (var i=0; i <x[0]; ++i){
        a="$a ";
    }

    return "$a${x[1]}";
}

        
String diamond(String c) {
    num numvalue = c.charCodeAt(0) - "A".charCodeAt(0);
    num i=0;    
    var a = [];

    while (i<=numvalue) {
        a.add([numvalue-i, diamond_line(new String.fromCharCodes([i+65]))]);
        i++;
    }

    for (i=numvalue-1; i>=0; --i) {
        a.add(a[i]);
    }

    return Strings.join(a.map((x) => (diam_line(x))), "\n");
}

main () {
    print(diamond("D"));
}

Note that I'm still learning these 2 languages, so there's a lot of room for improvement. CoffeeScript is close to Python, so I had less problems with it.

Offline

#4 October 20 2012

mesa177
Member

Re: [Exercise] Letter Diamonds

Matlab/Octave:

function diamond(letter)
if(nargin()-1)
  error("Incorrect number of arguments to function");
elseif (ischar(letter)&&~(size(letter,2)-1)) % Check if entry is a single character
      if ((65<=(letter-0))&&((letter-0)<=90)) % Check if entry is a capital letter
         if (letter-65)
            for i = 65:(letter-0)
                spaces1 = spaces2 = [];
                for j = 1:(letter-i) spaces1 = horzcat(spaces1,' '); end
                for j = 1:2*(i-65)-1 spaces2 = horzcat(spaces2,' '); end  
                if (i-65)
                   disp(horzcat(spaces1,char(i),spaces2,char(i)));
                else
                   disp(horzcat(spaces1,char(i)));
                end
                clear spaces1 spaces2;
            end    
            for i = (letter-1):-1:65
                spaces1 = spaces2 = [];
                for j = 1:(letter-i) spaces1 = horzcat(spaces1,' '); end
                for j = 1:2*(i-65)-1 spaces2 = horzcat(spaces2,' '); end  
                if (i-65)
                   disp(horzcat(spaces1,char(i),spaces2,char(i)));
                else
                   disp(horzcat(spaces1,char(i)));;
                end
                clear spaces1 spaces2;
            end
         else
            disp(letter);
         end  
      else
         error("The input character should be a capital letter");
      end
else
   error("The input character should be a capital letter");
end

Offline

#5 October 22 2012

Ayman
Member

Re: [Exercise] Letter Diamonds

A small exercise before sleep is nice, so here is my solution in Python, as concise as possible:

conv = lambda x: ord(str(x)[0].upper())

start = conv(raw_input("Start Letter: "))
end = conv(raw_input("End Letter: "))

charList = range(start, end)

rev = range(start, end+1)[::-1]

charList.extend(rev)

for i in xrange(0,len(charList)):
    
    mjust = lambda x: (i < len(rev) and (charList[i]-charList[0]+i)) or ((charList[i]-charList[0])*2)

    rchar = lambda x: ((chr(charList[i])) == chr(charList[0])) and (" ") or (chr(charList[i]))
                                        
    print  (" " * (len(charList)-(charList[i]-charList[0]))) + chr(charList[i])+ (" " * (mjust(0)-1)) + rchar(0)

Works with any 2 letters as long as first letter precedes the 2nd. It prints the diamond through a single loop where the justification is calculated relative to their distance within the list of integer values representing the characters, where the list contains all characters small to largest and back to small. Good night!

P.S. The -1 in (mjust(0)-1) is to compensate for the extra space returned from the rchar function in case the character is the first character e.g. for 'A' because returning an empty string or char such as ("') didn't work for me. I am relatively new to Python so any tips would be appreciated. I guess it just doesn't support empty strings...

Last edited by Ayman (October 22 2012)

Offline

#6 November 19 2012

CodingFreak
Member

Re: [Exercise] Letter Diamonds

Answer in C++:

#include <iostream>
using namespace std;
int main() {
	
	char letter[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
	char user_letter;
	int letter_number;

	//array of strings
	string diamond[100];

	//get the letter
	cout << "Input a letter: "; cin >> user_letter;

	//search for letter number in the array letter
	for (int i = 0; i < 26; i++) {
		if (letter[i] == user_letter) {
			letter_number = i;
			break;
		}
	}

	//construct diamond
	for (int i = 0; i < letter_number+1; i++) {
		
		//add initial spaces
		for (int j = 0; j < letter_number-i; j++) {
			diamond[i] += " ";
		}

		//add letter (first time)
		diamond[i] += letter[i];

		//add space between letters
		if (letter[i] != 'A') {
			for (int j = 0; j < 2*i-1; j++) {
				diamond[i] += " ";
			}
			//add letter (second time)
			diamond[i] += letter[i];
		}

		//add last spaces
		for (int j = 0; j < letter_number-i; j++) {
			diamond[i] += " ";
		}
	}

	//draw the diamond
	int j = 1;

	for (int i = 0; i < 2*letter_number+1; i++) {
		if (i < letter_number+1)
			cout << diamond[i];
		else {
			cout << diamond[i-2*j];
			j++;
		}
		cout << endl;
	}	
	return 0;
}

Last edited by CodingFreak (November 19 2012)

Offline

#7 November 26 2012

longbit
Member

Re: [Exercise] Letter Diamonds

Clear the way for JAVA.
i played around with ASCII, here is my code:

public class Diamonds {

    public static void main(String[] args) {
        int n,i,j,k;
        n=args[0].charAt(0)-65+1;
        for(i = 1; i<=n ; i++){
            for(j=1; j<=n-i;j++){
                System.out.print(" ");
            }
            System.out.print((char)(64+i));
            if(i>=2){
                for(k=1;k<=2*(i-2)+1;k++){
                    System.out.print(" ");
                }
                System.out.print((char)(64+i));
            }
            System.out.println();
        }
        for(i = n-1; i>=1 ; i--){
            for(j=1; j<=n-i;j++){
                System.out.print(" ");
            }
            System.out.print((char)(64+i));
            if(i>=2){
                for(k=1;k<=2*(i-2)+1;k++){
                    System.out.print(" ");
                }
                System.out.print((char)(64+i));
            }
            System.out.println();
        }
    }

}

Offline

Board footer