Problem of the Day
A new programming or logic puzzle every Mon-Fri

Party Hat

Over at Problem of the Day we like to party it up. Since April is coming to a close let's create a program to prepare for all the May birthdays coming up. Today's problem is given a number, generate a party hat that many lines tall. Here is an example with 8:

       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************

Permalink: http://problemotd.com/problem/party-hat/

Comments:

  • Tom - 10 years, 8 months ago

    a java solution:

    import java.util.Scanner;
    
    public class PartyHat{
        public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter size:");
        int size = sc.nextInt();
        System.out.println(makeHat(size));
        }
    
        public static String makeHat(int size){
        int layer = size;
        int count = layer - 1;
        int num = 1;
        char gap = ' ';
        char fill = '@';
        String out = "";
        while(layer > 0){
            for(int i = 0; i < count; i++){
            out += gap;
            }
            for(int j = 0; j < num; j++){
            out += fill;
            }
            out += "\n";
            num += 2;
            count--;
            layer--;
        }
        return out;
        }       
    }
    

    reply permalink

  • Anonymous - 10 years, 8 months ago

    In brainf*ck. online interpreter

    Height: ++++++++ (8)
    
    >>++++++[<++++++>-]<++++++>++++++++++>>++++[<++++++++>-]<<<<[->>>>+>+>+<<<<<<]
    >>>>>>[-<<<<<<+>>>>>>]+<-<[>[->>+>+>+<<<<]>>>>[-<<<<+>>>>]<<<<>[->>>+>+<<<<]>>
    >>[-<<<<+>>>>]<<<<>[-<<<<.>>>>]>>[-<<<<<<<<.>>>>>>>>]<[-<<<<<.>>>>>]<<<<<<.>>>
    ->++<<-]
    

    reply permalink

  • Jt - 10 years, 8 months ago

    Nicely done

    reply permalink

  • Anonymous - 10 years, 8 months ago

    class Program
    {
        static void Main(string[] args)
        {
            MakePartyHat();
        }
        static void MakePartyHat()
        {
            bool blnMakeHat = true; int intPartyHatSize; string strInput;
            while (blnMakeHat)
            {
                Console.WriteLine("Party hat size?");
                strInput = Console.ReadLine();
                if (Int32.TryParse(strInput, out intPartyHatSize))
                {
                    for (int i = 1; i <= intPartyHatSize; i++)
                    {
                        Console.WriteLine(new String(' ', intPartyHatSize - i) + new String('*', (i * 2) - 1));
                    }
                }
                if (strInput.ToLower() == "exit")
                {
                    blnMakeHat = false;
                }
            }
        }
    }
    

    reply permalink

  • Anonymous - 10 years, 8 months ago

    Nice little problem for a lazy afternoon. J:

    partyhat=: monad define
      (- |. i. y) |."0 1 '*' $~"0 >: +: i. y
    )
    

    reply permalink

  • Daniel - 10 years, 8 months ago

    I really need to start playing around with J.

    reply permalink

  • Anonymous - 10 years, 8 months ago

    It's good fun. I'm in an explainy mood today, so here's the gist of what that J snippet does:

    Calculate              Generate               Determine             Rotate
    star count           rows of stars            rotation             the rows
    
         1             '*              '             -7            '       *       '
         3             '***            '             -6            '      ***      '
         5             '*****          '             -5            '     *****     '
         7      =>     '*******        '      =>     -4     =>     '    *******    '
         9             '*********      '             -3            '   *********   '
        11             '***********    '             -2            '  ***********  '
        13             '*************  '             -1            ' ************* '
        15             '***************'              0            '***************'
    

    reply permalink

  • SFF - 10 years, 8 months ago

    Not as elegant as the others, but it works.

    import java.util.Scanner;
    
    class PartyHat
    {
        public static void main (String[] args)
        {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter party hat size:");
            int height = scanner.nextInt();
            makeHat(height);
        }
    
        public static void makeHat(int height)
        {
            int width = (2 * height) - 1;
            for (int i = 1; i <= height; i++)
            {
                for(int j = 1; j <= height - i; j++)
                    System.out.print(" ");
                for(int k = 1; k <= (2 * i) - 1; k++)
                    System.out.print("*");
                for(int l = 1; l <= height - i; l++)
                    System.out.print(" ");
    
                System.out.println();
            }
        }
    }
    

    reply permalink

  • duddles - 10 years, 8 months ago

    in python:

    def party_hat(n):
        for i in xrange(n):
            s = ' ' * (n - i - 1)
            s += '*' * (i*2 + 1)
            s += ' ' * (n - i - 1)
            print s   
    

    reply permalink

  • Anonymous - 10 years, 8 months ago

    dude why would you add trailing whitespace?

    reply permalink

  • Anonymous - 10 years, 8 months ago

    c#

    public static void Main() { Console.WriteLine(PartyHat(30)); }

    static string PartyHat(int height)
    {
        StringBuilder sb = new StringBuilder();
    
        int width = 2*height-1,
        lower = width/2, 
        upper = lower;
    
        while(lower >= 0)
        {
            for(int i = 0; i < width; i++)
            {
                if(i >= lower && i <= upper)
                    sb.Append("*");
                else
                    sb.Append(" ");
            }
            sb.AppendLine();
            lower--;
            upper++;
        }
        return sb.ToString();
    }
    

    reply permalink

  • Jt - 10 years, 8 months ago

    Trying some C++

    #include "stdafx.h"
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
    {
      string input = "";
    
      while (true)
      {
        int partyHatLines = 0;
    
        while (true) 
        {
          cout << "Enter the number of lines you would like in your party hat, or type exit" << std::endl;
          getline(cin, input);
    
          if (input == "exit")
          {
            return 0;
          }
    
          stringstream myStream(input);
          if (myStream >> partyHatLines)
          {
            break;
          }
          cout << "Invalid number, please try again" << endl;
        }
    
        int partyHatSpaces = partyHatLines + (partyHatLines - 1);
        for (int index = 1; index <= partyHatLines; index++)
        {
          int starCount = index + (index - 1);
          int padCount = (partyHatSpaces - starCount) / 2;
          cout << std::string(padCount, ' ');
          cout << std::string(starCount, '*');
          cout << std::string(padCount, ' ');
          cout << std::endl;
        }
      }
    
      return 0;
    }
    

    reply permalink

  • Dustin S - 10 years, 8 months ago

    Python 2.7

    def partyHat(lines):
      spaces = 0
      stars = lines * 2 - 1
    
      output = ""
      while (numberStars > 0):
        output = " " * numberSpaces + "*" * numberStars + "\n" + output
        numberSpaces += 1
        numberStars -= 2
    
      return output
    

    reply permalink

  • Dustin S - 10 years, 8 months ago

    lol, naming issues ^

    def partyHat(lines):
      spaces = 0
      stars = lines * 2 - 1
    
      output = ""
      while (stars > 0):
        output = " " * spaces + "*" * stars + "\n" + output
        spaces += 1
        stars -= 2
    
      return output
    

    reply permalink

  • Will - 10 years, 8 months ago

    Yaay more LISP! :D

    (defun make-party-hat-helper (i n)
      (when (> n 0)
        (do ((j 0 (+ j 1)))
        ((= j (- n 1)))
          (format t " "))
        (do ((j 0 (+ j 1)))
        ((= j i))
          (format t "*"))
        (format t "~%")
        (make-party-hat-helper (+ i 2) (- n 1))))
    
    (defun make-party-hat (n)
      (make-party-hat-helper 1 n))
    

    reply permalink

  • Samuel Chase - 10 years, 8 months ago

    Solution in Common Lisp.

    
    (defun print-party-hat (height)
      (flet ((hat-width (line-no)
               (+ (* 2 line-no) 1)))
        (let ((line-width (hat-width height)))
          (loop for line upto height do
               (let* ((hat-width (hat-width line))
                      (half-space-width (truncate (/ (- line-width hat-width) 2))))
                 (format t "~a" (make-string half-space-width :initial-element #\Space))
                 (format t "~a" (make-string (hat-width line) :initial-element #\*))
                 (format t "~a" (make-string half-space-width :initial-element #\Space))
                 (terpri))))))
    
    

    reply permalink

  • Byron - 10 years, 7 months ago

    This might be really bad but I am just learning Python.

    print("Please enter the number of rows you would like") rowcount = input(int)

    starCount = 1; for x in range(0, int(rowcount)): line = ' ' * (int(rowcount) - int(x)) line += '*' * int(starCount) starCount += 2 print(line)

    reply permalink

Content curated by @MaxBurstein