Hackerboard WikiHaboBlog

[HaBo]

 
Programmieraufgaben Hier wird regelmäßig eine neue Programmieraufgabe gestellt, die dann gelöst werden soll und in Zusammenarbeit mit den Moderatoren auch besprochen werden kann.

Buchstabenpyramide

Diskussion: Buchstabenpyramide im Forum Programmieraufgaben, in der Kategorie Code Kitchen; eingereicht von Ook! Zitat: Es soll eine Buchstabenpyramide erstellt werden. Dem Benutzer soll es möglich sein, einen Buchstaben einzugeben, auf ...

Antwort
Alt 05.05.08, 19:34   #1 (permalink)
CDW
Moderator
 
Benutzerbild von CDW
 
Registriert seit: 20.07.05
CDW Leistung: OpteronCDW Leistung: OpteronCDW Leistung: OpteronCDW Leistung: OpteronCDW Leistung: Opteron
Likes: 156
Standard Buchstabenpyramide


eingereicht von Ook!

Zitat:
Es soll eine Buchstabenpyramide erstellt werden.
Dem Benutzer soll es möglich sein, einen Buchstaben einzugeben, auf Basis dieses Buchstabens soll dann die Pyramide gebaut werden.

Beispiel:
Benutzer Eingabe >> "H"
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
Zusatzaufgabe: gib die Pyramide zentriert aus
Code:
  A
 ABA
ABCBA
__________________
Noch mal, für alle Pseudo-Geeks: 1+1=0. -> 10 wäre Überlauf!
Selig, wer nichts zu sagen hat und trotzdem schweigt.
CDW ist offline   Mit Zitat antworten
Alt 05.05.08, 21:33   #2 (permalink)
 
Registriert seit: 22.03.08
MontyPerl Leistung: Facit NTK
Likes: 0
Standard

yet anther perl oneliner   
Code:
$ echo h|perl -e 'sub Z{$_=join"",@_;$_.=reverse;y/a-z//s;return$_};$b=getc;for(a..$b){print" "x(ord($b)-ord),Z(a..$_),"\n"}'
       a
      aba
     abcba
    abcdcba
   abcdedcba
  abcdefedcba
 abcdefgfedcba
abcdefghgfedcba

chop($b = <>) in ein einfaches $b = getc umgeändert..
MontyPerl ist offline   Mit Zitat antworten
   
HaBOT
 

Werbung ist gerade online    
Alt 05.05.08, 22:37   #3 (permalink)
fetzer
Guest
 
Likes:
Standard

Java, formatiert   
Code:
import java.lang.*;
public class AlphPyr {
  public static void main(String[] args) {
    String alph="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int j,i,k;
    try{
	 for(i=0;i<alph.length();i++) {
	   for(k=alph.indexOf(args[0])-i;k>0;k--) System.out.print(" ");
	   for(j=0;j<=i;j++) System.out.print(alph.charAt(j));
	   for(j=i-1;j>=0;j--) System.out.print(alph.charAt(j));
	   System.out.println();
	   if(new Character(alph.charAt(i)).compareTo(args[0].toCharArray()[0]) == 0) break;
	 }
    }
    catch (ArrayIndexOutOfBoundsException e) {
	 System.out.println("[+] Bitte uebergeben sie dem Programm eine Basis. Beispielaufruf: java AlphPyr \"H\"");
    }
  }
}



Code:
fetzer:~> java AlphPyr "H"
       A
      ABA
     ABCBA
    ABCDCBA
   ABCDEDCBA
  ABCDEFEDCBA
 ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
  Mit Zitat antworten
Alt 21.05.08, 03:56   #4 (permalink)
 
Registriert seit: 21.04.08
Ook! Leistung: Facit NTK
Likes: 0
Standard

Hallo!

Meine Java Lösung

Code:
public class PyraChars {

	private int maxChars;
	private char[] chars = { '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' };
	
	/**
	 * @param value
	 */
	public PyraChars(int value) {
		System.out.println();
		if(value > 90)
			value -= 32;
		value -= 64;
		maxChars = value;
		
		createPyra(1);  	
	}
	
	/**
	 * @param i Grenze
	 */
	private void createPyra(int i) {		
		int charsRow = ((i * 2) - 1); 
		String s = "";
		
		for(int z=0; z<(maxChars-i); z++)
			s += " ";

		for(int z=0; z<charsRow; z++) {				
			if(z < (charsRow / 2))
				s += chars[z];
			else if(z == (charsRow / 2))
				s += chars[(charsRow / 2)];
			else
				s += chars[(charsRow - z - 1)];
		}
		
		System.out.println(s);
		if(i < maxChars)
			createPyra(i+1);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("   Buchstaben Pyramide\n-------------------------"); 
		System.out.print("Buchstabe eingeben: "); 
		new PyraChars(readValue());
	}

	/**
	 * @return value
	 */
	public static int readValue() {
		try { return new BufferedReader(new InputStreamReader(System.in)).read(); }
		catch(IOException ex) { return 0; }
	}
	
}
Gruß
Felix
Ook! ist offline   Mit Zitat antworten
Alt 21.05.08, 18:43   #5 (permalink)
 
Benutzerbild von Eydeet
 
Registriert seit: 14.04.06
Eydeet Leistung: Facit NTK
Likes: 4
Standard

Ich hab's mal als Ansporn genommen, um Brainfuck zu lernen
Code:
++++++++++>++++++++[>++++++++<-]>>,>+++++[<------------->-]<+[>+[>+>+>+<<<-]>>>[<<<+>>>-]<<<>[<<<+.>>>-]>-[<<<<-.>>>>-]<<<<-<<.>>>-]
Das geht bestimmt kürzer und die Pyramide wird nicht zentriert, aber: es funktioniert!
Eydeet ist offline   Mit Zitat antworten
Alt 01.08.08, 04:50   #6 (permalink)
 
Registriert seit: 01.08.08
TheCrap Leistung: Facit NTK
Likes: 0
Standard

ist zwar schon was her aber die anderen aufgaben die ich gemacht hab sind noch älter
hier meine lösung in python

Code:
l=[]
m=[65]
n=[]
i=0
out=""

a= raw_input("Bitte einen Buchstabe von a-z eingeben: ")
a=a.upper()
if len(a)== 1 and ord(a) in range(65,91):
    b=ord(a)-65
    if a == "A":
        print "A"
    else:
        print " "*b+"A"
        while a not in out:
            m += [m[i]+1]
            n = m[:]
            n.reverse()
            del n[0]
            l=m[:]
            l.extend(n)
            j = 0
            i += 1
            out=" "*(b-i)
            while j < len(l):
                out +=chr(l[j])
                j += 1     
            print out
else:
    print "Ich hab gesagt EINEN BUCHSTABE, du Lulli!"
hab vor nicht all zu langer zeit angefangen und bin für tips und verbesserungsvorschläge immer dankbar
TheCrap ist offline   Mit Zitat antworten
Alt 08.11.08, 16:38   #7 (permalink)
 
Registriert seit: 22.03.08
MontyPerl Leistung: Facit NTK
Likes: 0
Standard

Auch mal in brainfuck: (wirklich gute Aufgabe zum brainfuck-Lernen!)
Code:
,>>++++++++[-<++++++++>>>+<<]>>++<<<+[-<->>>+<<]<[>[-<+>>+<]>[-<<->+>]<[->+>.+<<]>>.<[-<+>>-.<]>>.<<+<<]
Edit:
Oder zentriert:
Code:
,>>++++++++[-<++++++++>>>+>++++<<<]>>++<<<+[-<->>>+<<]>>>>.<<<<<[[->>>>>>+<.<<<<<]>>>>>>[-<<<<<<+>>>>>>]<<<<<[-<+>>+<]>[-<<->+>]<[->+>.+<<]>>.<[-<+>>-.<]>>.<<+<<]
MontyPerl ist offline   Mit Zitat antworten
Alt 08.11.08, 18:13   #8 (permalink)
Moderator
 
Benutzerbild von xeno
 
Registriert seit: 09.09.04
xeno Leistung: 8086xeno Leistung: 8086
xeno eine Nachricht über ICQ schicken
Likes: 65
Standard

das ganze in C# :

Code:
using System;
class BuchstabenPyramide {
    public static void Main(string[] args) {
        int Spitze = (int)(Convert.ToChar(args[0]));
        int Stufen = Spitze - 65; 
        int w,x,y,z;
        for(x=0; x<=Stufen; x++) {
            for(w=0; w<Stufen-x; w++) { // Leerzeichen
                Console.Write(" ");
            }   
            for(y=0; y<=x; y++) { // Vorwärts
                Console.Write("{0}", Convert.ToChar(y+65));
            }   
            for(z=y-2; z>=0; z--) { // ... und Rückwärts
                Console.Write("{0}", Convert.ToChar(z+65));
            }   
            Console.WriteLine("");
        }   
    }   
}
Ausgabe:
Code:
18:12 xeno@gideon:~/source/csharp/buchstabenpyramide mono bp.exe H
       A
      ABA
     ABCBA
    ABCDCBA
   ABCDEDCBA
  ABCDEFEDCBA
 ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
xeno ist gerade online   Mit Zitat antworten
Alt 08.11.08, 22:43   #9 (permalink)
Senior Member
 
Benutzerbild von t3rr0r.bYt3
 
Registriert seit: 07.01.03
t3rr0r.bYt3 Leistung: Z3
Likes: 13
Standard

Ich konnts mir nicht verkneifen, das mal schnell zusammenzuhacken. Den Array-Inhalt hab ich natürlich nicht von Hand geschrieben, sondern mir generieren lassen - ich hab also die Aufgabe tatsächlich gelöst

   
Code:
public class Pyr {

private static char[][] chars = new char[][] {
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' ',' '},
{' ',' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' ',' '},
{' ',' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' ',' '},
{' ',' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' ',' '},
{' ',' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' ',' '},
{' ','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','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A',' '},
{'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','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A'}
};

        public static void main(String[] args){
                for (int i = 0; i < (int)args[0].charAt(0)-64; i++){
                        for (int j = 0; j < 51; j++){
                                System.out.print(chars[i][j]);
                        }
                System.out.println();
                }
	}
}
t3rr0r.bYt3 ist offline   Mit Zitat antworten
Alt 09.11.08, 12:16   #10 (permalink)
 
Registriert seit: 07.03.08
90nop Leistung: Facit NTK
Likes: 0
Standard

Hier mal etwas exotisch in AHK:
Funktion, die eine Variable mit der Buchstabenpyramide zurückgibt. (Eingemittet)

Code:
pyramid_alpha($char){
	Loop, % len := asc($char) - 64
	{
		Loop, % len - strlen(prev_last)
			_space .= a_space 
		c := chr(65 + A_index - 1)
		line .= _space . prev_last . c . suff_last "`n"
		prev_last	.=  c, suff_last :=  c . suff_last, _space := ""
	}
return, line	
}
90nop ist offline   Mit Zitat antworten
Alt 02.01.09, 04:15   #11 (permalink)
 
Registriert seit: 02.01.09
xhugox Leistung: Facit NTK
Likes: 0
Standard

Ich hab das mal in C++ geschrieben:
C++ Code   

Code:
/* A
ABA
ABCBA
ABCDCBA
*/
#include <iostream>
using namespace std;

char CinBuch[100];
char kurz[2];
char BSalat[100];
int Array = 0;
bool Buch = false;

int main(int argc, char *argv[]) {
BSalat[Array] = 'A';
cout << "Grossbuchstabe eingeben" << endl;
cin >> CinBuch;
int Oft;
Oft = (int) CinBuch[0] - (int) 'A';

int line = 0;
while (true) {//bis CinBuch[0]
kurz[0] = 'A';
kurz[0] = (int) kurz[0] + line;
int j = 0;
j = j + line;

if (kurz[0] == CinBuch[0]) {
Buch = true;
}

for (; j < Oft; j++) {
cout << " ";
}
for (; (int) BSalat[Array] < (int) kurz[0];) {
Array++;
BSalat[Array] = BSalat[Array - 1] + 1;
}
for (; BSalat[Array] != 'A';) {
Array++;
BSalat[Array] = BSalat[Array - 1] - 1;
}
cout << BSalat << endl;
Array = 0;
line++;
if (Buch) {
system("PAUSE");
return EXIT_SUCCESS;
}
}
}


Wenn euch irgendetwas auffällt, was man unbedingt ändern sollte, zögert nicht mir eine PN zu schicken.
xhugox ist offline   Mit Zitat antworten
Alt 02.01.09, 12:26   #12 (permalink)
 
Registriert seit: 26.12.08
Athelstan Leistung: Facit NTK
Athelstan eine Nachricht über ICQ schicken
Likes: 0
Standard

Meins in Ruby, relativ kurz:

Code:
argv = ARGV[0]; i = 1; a = ("A".."Z").to_a; j = 0
a.each { |part|
	if part == argv
		j = i+1
	end
	i += 1
}
i = 1; k = 0; l = j-1
while i < j
	print (" "*l)
	i.times do
		print "#{a[j-(j-k)]}"; k += 1
	end
	(i-1).times do
		print "#{a[j-(j-(k-2))]}"; k -= 1
	end
	puts; i+=1; k = 0; l -= 1
end
Ausgabe:

Code:
athelstan@127.0.0.1:~$ ruby Buchstabenpyramide.rb O
               A
              ABA
             ABCBA
            ABCDCBA
           ABCDEDCBA
          ABCDEFEDCBA
         ABCDEFGFEDCBA
        ABCDEFGHGFEDCBA
       ABCDEFGHIHGFEDCBA
      ABCDEFGHIJIHGFEDCBA
     ABCDEFGHIJKJIHGFEDCBA
    ABCDEFGHIJKLKJIHGFEDCBA
   ABCDEFGHIJKLMLKJIHGFEDCBA
  ABCDEFGHIJKLMNMLKJIHGFEDCBA
 ABCDEFGHIJKLMNONMLKJIHGFEDCBA
Athelstan ist offline   Mit Zitat antworten
Alt 06.04.09, 20:14   #13 (permalink)
 
Registriert seit: 21.04.08
Ook! Leistung: Facit NTK
Likes: 0
Standard

eine nette Python Lösung

Code:
from itertools import takewhile

def row(c, limit):
  rowSpace = (alph.find(limit) - alph.find(c) - 1) * " "
  row = "".join(takewhile(lambda x: x != c, alph))
  return rowSpace + row + row[::-1][1:] + "\n"

def pyra(limit):
  limit = chr(ord(limit)+2)
  return "".join(map(lambda c: row(c, limit), takewhile(lambda x: x != limit, alph)))[:-1]

alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print pyra('F')
... und eine Haskell Lösung

Code:
import Data.Char
import Data.List

doIt :: Char -> [String]
doIt limit = map row $ takeWhile (/= limit) $ alph
  where
    alph = ['A'..'Z']
    row c = (rowSpace c 0) ++ (rowAlph c) ++ (drop 1 $ reverse $ rowAlph c)
    rowAlph c = takeWhile (/= c) $ alph
    rowSpace c i = if i == rowSpaceCnt c then "" else ' ' : rowSpace c (i+1)
    rowSpaceCnt c = ((index (limit, 0)) - (index (c, 0)) - 1)
    index (c, i) = if i > 25 then 1 else if alph !! i == c then i else index (c, i+1)

pyra :: Char -> String
pyra limit = concat . intersperse "\n" $ doIt (chr $ ord limit + 2)

main :: IO()
main = putStrLn $ pyra 'F'
Gruß
Felix
Ook! ist offline   Mit Zitat antworten
Alt 13.04.09, 02:27   #14 (permalink)
 
Registriert seit: 12.01.09
lone.wolf Leistung: Z3
lone.wolf eine Nachricht über AIM schicken
Likes: 1
Standard

ne Delphi Version

Code:
// Delphi
type
  TA_Z = 'A'..'Z';

function CharPyramid( const Input: TA_Z ): String;
var
  i, j,
  k, l,
  m: Integer;
begin
  j := 0;
  k := ORD(Input)-64;
  l := -1;
  // Summe der Folge Ai = i*2+1 berechnen
  for i := 1 to k do
    inc( j, i*2+1 );
  // Result = Länge - Letzte LFCR (#13#10)
  SetLength( Result, j-2 );
  for j := 1 to k do
  begin
    m := (j-1)*2;
    inc( l, m+1 );
    inc( m, 2 );
    for i := 1 to j do
    begin
      Result[l+i]   := chr(64+i);
      Result[l+m-i] := chr(64+i)
    end;
    if j<k then
    begin
      Result[l+m]   := #13;
      Result[l+m+1] := #10;
    end;
  end;
end;

function Center( Input: String ): String;
var
  i, j,
  k, l: Integer;
begin
  j := 1;
  for i := 1 to Length(Input) do
    if input[i] = #13 then
      inc(j);
  for i := 1 to j do
  begin
    l := 0;
    if i>1 then
      for k := 1 to Length(Input) do
      begin
        if Input[k] = #13 then
        begin
          inc( l );
          if l = i-1 then
          begin
            l := k+2;
            break;
          end;
        end;
      end;
    Insert( StringOfChar( ' ', j-i ), Input, l );
  end;
  Result := Input;
end;

// Möglicher Aufruf:
// (~Font = Courier New)
Center( CharPyramid( 'Z' ) );
Code:
                         A
                        ABA
                       ABCBA
                      ABCDCBA
                     ABCDEDCBA
                    ABCDEFEDCBA
                   ABCDEFGFEDCBA
                  ABCDEFGHGFEDCBA
                 ABCDEFGHIHGFEDCBA
                ABCDEFGHIJIHGFEDCBA
               ABCDEFGHIJKJIHGFEDCBA
              ABCDEFGHIJKLKJIHGFEDCBA
             ABCDEFGHIJKLMLKJIHGFEDCBA
            ABCDEFGHIJKLMNMLKJIHGFEDCBA
           ABCDEFGHIJKLMNONMLKJIHGFEDCBA
          ABCDEFGHIJKLMNOPONMLKJIHGFEDCBA
         ABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBA
        ABCDEFGHIJKLMNOPQRQPONMLKJIHGFEDCBA
       ABCDEFGHIJKLMNOPQRSRQPONMLKJIHGFEDCBA
      ABCDEFGHIJKLMNOPQRSTSRQPONMLKJIHGFEDCBA
     ABCDEFGHIJKLMNOPQRSTUTSRQPONMLKJIHGFEDCBA
    ABCDEFGHIJKLMNOPQRSTUVUTSRQPONMLKJIHGFEDCBA
   ABCDEFGHIJKLMNOPQRSTUVWVUTSRQPONMLKJIHGFEDCBA
  ABCDEFGHIJKLMNOPQRSTUVWXWVUTSRQPONMLKJIHGFEDCBA
 ABCDEFGHIJKLMNOPQRSTUVWXYXWVUTSRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRSTUVWXYZYXWVUTSRQPONMLKJIHGFEDCBA
lone.wolf ist offline   Mit Zitat antworten
Alt 14.04.09, 15:25   #15 (permalink)
 
Benutzerbild von pytohn
 
Registriert seit: 28.03.09
pytohn Leistung: Facit NTK
Likes: 0
Standard

Hier mal vorab unformatiert in Python 3. Ich glaube, dass ginge auch kürzer


Code:
alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

def turn(str):
    try:
        i = 1
        len_ = len(str)
        while i < len_:
            str = str + str[(len_ - i -1):(len_ - i)]
            i = i +1
        return str
    except:
        print("Leider trat ein Fehler auf!")

def pyramide():
    try:
        zentrum = input("Mitte der untersten Reihe: ")
        i = 0
        while i <= alph.index(zentrum):
            print(turn(alph[:(i+1)]))
            i = i+1
    except:
        print("Leider trat ein Fehler auf!")

pyramide()
pytohn ist offline   Mit Zitat antworten
Antwort
   

Werbung ist gerade online    

[HaBo] » Software Home » Code Kitchen » Programmieraufgaben » Buchstabenpyramide
Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind aus
Pingbacks sind aus
Refbacks sind aus



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61