Last Python Intro Challenges

Seems like in the past couple weeks I have been getting up earlier than usual. I decided to complete the remaining introduction Python challenges at HackerRank. If you are interested please take a look at the following URL:  https://www.hackerrank.com/domains/python/py-introduction

For these challenges I decided to use Spyder 3 IDE. I am using:  Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]

Arithmetic Operators

Sample input and associated output:

3 <== input

2 <== input

5

1

6

My code follows:

# **** ****

if __name__ == ‘__main__’:

# **** read inputs ****

a = int(input())

b = int(input())

#**** compute and print results ****

print(a + b)

print(a – b)

print(a * b)

The two test cases passed.

Loops

Sample input and associated output:

5 <== input

0

1

4

9

16

My code follows:

if __name__ == ‘__main__’:

n = int(input())

# **** ****

for i in range(0, n):

print(i * i)

The code passed the two test cases.

Write a function

Sample input and associated output from the IDE console:

1900 <== input (year)

False <== output (was NOT a leap year)

My code follows:

def is_leap(year):

# **** for starters ****

leap = False

# **** divisible by 4****

if ((year % 4) == 0):

# **** divisible by 100 ****

if ((year % 100) == 0):

# **** divisible by 400 ****

if ((year % 400) == 0):

leap = True

else:

leap = False;

else:

leap = True

# **** ****

return leap

The code passed the six test cases.

Print Function

The sample input and my output follows:

3

123

My Python code follows:

if __name__ == ‘__main__’:

n = int(input())

# **** ****

for i in range(1, n + 1):

print(i, end=””)

The code passed the three test cases.

At this point I am done with the Introduction challenges. Next I will tackle the Basic Data Types. It seems like they are also all grouped at the Easy level.

If you have a comment on any post in this blog please do not hesitate and send me a message. Will reply ASAP and will not mention your name unless you explicitly allow me to do so.

John

john.canessa@gmail.com

Follow me on Twitter: @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *