Jungletronics
Published in

Jungletronics

CS50 — A Taste of Python

Harvard Mario’s Challenge Solver \o/ #PurePythonSeries — Episode #10

Hi! Let’s have fun! Just that! Let’s go to the amusement park and play with python?

This time, I’m taking the CS50 — from Harvard University.

Solving CS50 — Pset1 — Exercise — Class 1Links:
https://materiais.napratica.org.br/cc50
https://www.napratica.org.br/curso-de-harvard-traduzido-cc50/
https://cs50.harvard.edu/x/2021/

Be welcome!

Let’s get started!

We need for loop:

for(i = 0; i < n; i++):print('#')File "<ipython-input-82-101f42122273>", line 1
for(i = 0; i < n; i++):
^
SyntaxError: invalid syntax

Opps! In Python, there is not C like syntax for(i=0; i<n; i++) but you use for in n.

If you want to automate a specific repetitive task or prevent yourself from writing repetitive code in your programs, using a loop is the best option for that.

Loops are a set of instructions that run repeatedly until a condition is met.

To solve these problems, we surely need one:

Create in Python this Mario`s Pyramid in console…
…and these 2 pyramids back-to-back

There are two types of loops built into Python:

for loops:


for iterator in sequence:
execute expression

while loops:


while condition:
execute expression

Let’s begin try in Python fashion way:

for number in range(10, 21):
if number % 2 == 0:
print("The number", number, "is even")
[out]The number 10 is even
The number 12 is even
The number 14 is even
The number 16 is even
The number 18 is even
The number 20 is even

Here is # Tuple Unpacking:

x = [(1,2),(3,4),(5,6)]for item in x:
print(item)
[out](1, 2)
(3, 4)
(5, 6)

Or:

for (a,b) in x:
print(a)
[out]1
3
5

Or even:

for a,b in x:
print(a+1, b+1)
[out]2 3
4 5
6 7

Let’s review # Python end parameter in print() too:

print("Welcome to" , end = ' ')print("GeeksforGeeks", end = ' ')
[out]
Welcome to GeeksforGeeks

It ends line by space instead of \n.

Look here we did not call parameter in the first print:

print("Welcome to")print("GeeksforGeeks", end = ' ')[out]Welcome to
GeeksforGeeks

MARIO'S PIRAMID

for inside for

# Mario's pyramid upside down :)for i in range(0, 4):
for j in range(0,4-i):
print('#', end=" ")
print('')[out]# # # #
# # #
# #
#

Now :

# Mario's pyramid left alignment:)for i in range(0, 4):
for j in range(0,i+1):
print('#', end=" ")
print('')[out]#
# #
# # #
# # # #

Just while loop to ask and for to print

# Mario's Pyramid right alignment:)
# asks numbers between 0 and 8
print('Height: ', end=' ')
h = int(input());
while(h<0 or h > 8):
print("That is an invalid input")
print('Height: ', end='')
h = int(input())
for i in range(h):
print(" "*(h-i)+"#"*(i+1))

[out]
Height: 4
#
##
###
####

A different for:

# The counter's range is toyed with here
for
i in range(1, h + 1):
# The "i + 1" is removed
print(" " * (h - i) + "#" * i)
[out] #
##
###
####

Step by step solution:

#first:

# Printing spacesh=4
for i in range(h):
print(" "*(h-i));
[out]

Single Hash by line:

# Printing 1 hash at the end of spacesh=4
for i in range(h):
print(" "*(h-i)+"#")
[out] #
#
#
#

Final result:

# Printing the whole Mari's Piramidh=4
for i in range(h):
print(" "*(h-i)+"#"*(i+1))
[out] #
##
###
####

Double Pyramid:

print('Height: ', end='');h = int(input());while(h<0 or h > 8):
print("That is an invalid input")
print('Height: ', end='');
h = int(input());
for i in range(h):
print(" "*(h-i)+"#"*(i+1)+ " "+"#"*(i+1));

[out]
Height: 4
# #
## ##
### ###
#### ####

Enough o/

print("That's it, folks. This is CS50! Harvard course, man \o/")That's it, folks. This is CS50! Harvard course, man \o/

That’s all, folks!

See you soon o/

Bye!

👉Jupiter notebook link :)

👉git

Credits & References

Harvard University

Fundacao Estudar

Related Posts

00#Episode#PurePythonSeries — Lambda in Python — Python Lambda Desmistification

01#Episode#PurePythonSeries — Send Email in Python — Using Jupyter Notebook — How To Send Gmail In Python

02#Episode#PurePythonSeries — Automate Your Email With Python & Outlook — How To Create An Email Trigger System in Python

03#Episode#PurePythonSeries — Manipulating Files With Python — Manage Your Lovely Photos With Python!

04#Episode#PurePythonSeries — Pandas DataFrame Advanced — A Complete Notebook Review

05#Episode#PurePythonSeries — Is This Leap Year? Python Calendar — How To Calculate If The Year Is Leap Year and How Many Days Are In The Month

06#Episode#PurePythonSeries — List Comprehension In Python — Locked-in Secrets About List Comprehension

07#Episode#PurePythonSeries — Graphs — In Python — Extremely Simple Algorithms in Python

08#Episode#PurePythonSeries — Decorator in Python — How To Simplifying Your Code And Boost Your Function

10#Episode#PurePythonSeries — CS50 — A Taste of Python — Harvard Mario’s Challenge Solver (this one)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store