awk diamond code golf (just for fun!)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk diamond code golf (just for fun!)
Prev   Next
# 1  
Old 11-26-2016
awk diamond code golf (just for fun!)

Hey guys,

This is purely just a little bit of fun with awk. I realize this this isn't that constructive so please remove if need be.

Your goal:
Create a one line awk script that generates a diamond shape of any
size. Both the size of the diamond (measured by its middle line) and
the character used ("*" in the examples) should be stored in variables
that can be changed easily. Have fun!


Example diamond with the size of 5:
Code:
  *
 ***
*****
 ***
  *

Example diamond with the size of 10:

Code:
    **
   ****
  ******
 ********
**********
 ********
  ******
   ****
    **

My solution is below - I didn't spend too long on it so I imagine it can be improved a lot more.
Obviously DON'T LOOK HERE if you want to try this yourself from scratch without any clue!:

Code:
awk 'function g(n,v){V="";for(i=1;i<=n;i++)V=V?V sprintf("%s",v):sprintf("%s",v)}BEGIN{L=15;v="*";B=L%2?1:2;s=(L-B)/2;while(B>0){g(B,v);D=V;g(s," ");S=V;printf"%s%s%s\n", S,D,S;if(B==L){f=1};B=f?B-=2:B+=2;s=f?s+=1:s-=1}}'

These 4 Users Gave Thanks to pilnet101 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To print diamond asterisk pattern based on inputs

I have to print the number of stars that increases on each line from the minimum number until it reaches the maximum number, and then decreases until it goes back to the minimum number. After printing out the lines of stars, it should also print the total number of stars printed. I have tried... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

2. What is on Your Mind?

Place your bets! - Ryder Cup 2012 (Golf)

Who will win the 2012 Ryder Cup. Europe vs USA There is an open event in the Event Prediction Forum. The event closes on 27th Sep 2012. 2012 Ryder Cup - Wikipedia, the free encyclopedia (0 Replies)
Discussion started by: ni2
0 Replies

3. UNIX for Dummies Questions & Answers

fun and easy awk question

I have a file called mytitles.txt containing a list of book titles I have a second file called abfile.txt containing a list of book titles (in the 3rd field) and it has author info and copyright year info as well.. I want to search mytitles.txt for a match in the 3rd field of abfiles.txt, and... (2 Replies)
Discussion started by: glev2005
2 Replies

4. Shell Programming and Scripting

Perl Diamond Operator

I know that when using 'while (<FILE>) {}', Perl reads only one line of the file at one time, and store it in '$_'. Can I change some parameters so that 'while (<>) {}' can read more than one lines, like 2 or 5 lines at one time? Thanks for the help! (1 Reply)
Discussion started by: zx1106
1 Replies

5. Shell Programming and Scripting

More fun with awk

#!/usr/bin/ksh ls -l $@ | awk ' /^-/ { l = 5*log($5) h = sprintf("%7d %-72s",$5,$8) print "\x1B ls command with histogram of file sizes. The histogram scale is logaritmic, to avoid very short bars for smaller files or very long bars for bigger files. Screenshot: (4 Replies)
Discussion started by: colemar
4 Replies

6. Shell Programming and Scripting

Fun with awk

uggc://ra.jvxvcrqvn.bet/jvxv/EBG13 #!/usr/bin/awk -f BEGIN { for (n=0;n<26;n++) { x=sprintf("%c",n+65); y=sprintf("%c",(n+13)%26+65) r=y; r=tolower(y) } } { b = "" for (n=1; x=substr($0,n,1); n++) b = b ((y=r)?y:x) print b } ... (0 Replies)
Discussion started by: colemar
0 Replies

7. Shell Programming and Scripting

Diamond operator in Until Statement (perl)

Hello: I have the following perl script which is giving me trouble inside the second elsif statement. The purpose of the script is to go through a file and print out only those lines which contain pertinent information. The tricky part came when I realized that certain items actually spanned... (5 Replies)
Discussion started by: erichpowell
5 Replies
Login or Register to Ask a Question