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