Jump to content

Okay guys, I was going through some old python programs of mine and i would like to see some other, better methods to solve this problem.

 

The Challenge!!

You must make a program that outputs a diamond pattern, where 'n' as the maximum width of the diamond.

E.g.

 

n = 5    *   * *  * * * * * * ** * * * * * * * *  * * *   * *    *n = 3  * * ** * * * *  *
width = int(input("Enter width: "))loopPassA = 1loopPassC = 1maxPasses = width * 2 - 1line = ""while loopPassA <= maxPasses:if loopPassA <= width:spacesNeeded = width - loopPassAstarsNeeded = loopPassAline = ""loopPassB = 0while len(line) < spacesNeeded:line = line + " "while loopPassB < starsNeeded:line = line + "* "loopPassB = loopPassB + 1print(line.rstrip())if loopPassA > width:spacesNeeded = loopPassCstarsNeeded = width - loopPassCline = ""loopPassB = 0while len(line) < spacesNeeded:line = line + " "while loopPassB < starsNeeded:line = line + "* "loopPassB = loopPassB + 1loopPassC = loopPassC + 1print(line.rstrip())loopPassA +=1STOP = input("STOP")

Works with python 3.4

 

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
https://linustechtips.com/topic/317759-diamond-challenge/
Share on other sites

Link to post
Share on other sites

Sorry this is out of my nerd comfort zone :D

#LinusKitchenTips /// "Better than useless" - Linus Sebastian

LTT Holy bible: Code Of Conduct

Project Toaster [My Silver NCASE M1 V2 Build-log] 

Main Rig
 Case: Fractal Design Define R5 CPU: INTEL 
i5 3570k Cooler: CM Hyper 212 Evo Mobo: Maximus V Gene Z77 GPU: Gigabyte GTX 970 G1 Gaming (w/ 0% fan mode) RAM: Corsair Dominator Platinum 2x8GB 1600mHz Storage: OCZ VERTEX 4 256GB PSU: Corsair AX860 Monitor: ASUS PB278Q 1440p 27" Headphones: QPAD QH-90 Laptop
Macbook Pro Retina 13" i5 256Gb Early 2015
Phone
Oneplus One 64GB Sandstone Black
Link to comment
https://linustechtips.com/topic/317759-diamond-challenge/#findComment-4318898
Share on other sites

Link to post
Share on other sites

(display "n: ")

(let ([width (string->number (read-line))])

(for ([i (in-range 1 (* 2 width))])

(let ([stars (if (> i width) (- width (abs (- width i))) i)])

(display

(string-append

(make-string (- width stars) #\space)

(apply string-append (make-list stars "* "))

"\n")))))

Link to comment
https://linustechtips.com/topic/317759-diamond-challenge/#findComment-4319396
Share on other sites

Link to post
Share on other sites

Well those answers are ridiculously simple compared to my method.

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
https://linustechtips.com/topic/317759-diamond-challenge/#findComment-4326999
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×