[PYTHON] Programming improvement course 1

Even if you learn the grammar of the program language How to write I think there are many people who still don't understand.

This is a programming improvement course for such people. If you want to think, stop the video Think of a program.

Click here for the explanation video

I'm sorry if it doesn't appear

Try to write a star

The theme this time is try to write a star. Let's create a function that draws a star like this:


conditions:
n =Number of lines
def star(n):
processing

Beginner's problem

Draw n stars in the nth stage

For example

star(5)

After executing, it looks like the following Let's create a function that will be displayed.

★ ★★ ★★★ ★★★★ ★★★★★

Intermediate problems

Draw a star in n steps to form a triangle

star(5) 

Mouth Mouth Mouth Mouth Mouth Mouth Mouth Mouth Mouth Mouth Mouth Mouth ★★★★★ Mouth Mouth ★★★★★★★ Mouth ★★★★★★★★★

Advanced issues

Draw a star like drawing a diamond in n steps

star(9)

Mouth Mouth Mouth Mouth Mouth Mouth Mouth Mouth Mouth ★ Mouth Mouth Mouth Mouth ★ Mouth Mouth ★ Mouth Mouth ★ Mouth Mouth Mouth Mouth ★ Mouth ★ Mouth Mouth Mouth Mouth Mouth ★ Mouth ★ Mouth Mouth Mouth Mouth ★ Mouth Mouth Mouth ★ Mouth Mouth ★ Mouth Mouth Mouth Mouth ★ Mouth Mouth Mouth Mouth Mouth Mouth Mouth Mouth

star(10)

Mouth Mouth Mouth Mouth Mouth Mouth Mouth Mouth ★ Mouth Mouth ★ Mouth Mouth Mouth Mouth ★ Mouth Mouth Mouth ★ Mouth Mouth ★ Mouth Mouth Mouth Mouth Mouth ★ Mouth ★ Mouth Mouth Mouth Mouth Mouth Mouth ★ ★ Mouth Mouth Mouth Mouth Mouth Mouth ★ Mouth ★ Mouth Mouth Mouth Mouth Mouth ★ Mouth Mouth Mouth ★ Mouth Mouth Mouth ★ Mouth Mouth Mouth ★ Mouth Mouth ★ Mouth Mouth Mouth Mouth Mouth Mouth Mouth Mouth

Now let's think about it.

Beginner's answer

First, you can print the characters with the print function.

Because there are n stars in the nth stage Because the number changes every time

To do this kind of repetition Use the for statement.

def star(n):
    for i in range(n):
        print('★'*(i+1))
star(5)

With the range function You can generate alternatives to numbers.

The numbers start at 0, so add 1 to them.

When it comes to characters in Python Because you can express repetition with * Use the print function to print characters for n steps If you display it, it is completed.

Intermediate answer

First, find the total amount based on the number of lines entered as an argument. The number of stars output in a pyramid shape is number of rows x 2 -1.

Then output

Mouth on the left side of the star Mouth on the right side of the star

Divide it into 3 parts and calculate the number of each.

Once the number of stars is decided, subtract it from the whole Divide by 2 to get the number of outer mouths.

It became such a function.

def star(n):
    tmp = n*2-1
    for i in range(n):
        s = ((i+1)*2-1)
        k = (tmp - s)//2
        res = 'mouth'*k + '★'*s + 'mouth'*k
        print(res)
        
star(5)

Advanced answer

I think it will take time for advanced players to solve it.

First, when the number of entered lines is odd The process changes when the number is even.

At the top and bottom of the diamond Note that the process will change.

Rhombus left mouth Outer frame and mouth of ★ for drawing in a diamond shape Rhombus right mouth

Think separately If you calculate the number of each part and connect You can reach the correct answer.

Consider the number first in the conditional branch of the if statement Consider one line of characters to output.

You can draw a star with a code like this.


def star(n):
    for i in range(n):
        s = (i+1)*2-1        
        k = (i-n//2) if (i+1>n//2) else (n - s)//2
        w = (n-(k*2)-2) if (i+1>n//2) else (i*2 if n%2==0 else i*2-1) 
        res = 'mouth'*k
        if (i+1)==1 or (i+1)==n:
            res += '★★' if n%2==0 else '★'
        else:
            res += '★' + 'mouth'*w + '★'
        res +='mouth'*k
        print(res)

Since it is a reference example, it is correct if there is a result.

Summary

How was it?

The idea of programming Did you come along?

I would like to continue doing it For those who want to improve their programming Please try to solve it.

Well then.

Author information

Otsu py's HP: http://www.otupy.net/

Youtube: https://www.youtube.com/channel/UCaT7xpeq8n1G_HcJKKSOXMw

Twitter: https://twitter.com/otupython

Recommended Posts

Programming improvement course 1
Programming improvement course 2
Programming Improvement Course 4: Pixel Logic
Poop programming
Graphic programming
Shell programming