Introduction to Programming with Python: Problem and Solution

Introduction to Programming with Python: Problem and Solution

This one is for the beginners. Who just started learning programming and started with Python. Programming requires constant practice and lots of patience. I did this course on Udacity “Intro to Computer Science”. It’s a great course for beginners. It teaches all the programming basics very well. I am sure many people are taking this course. I just wanted to be helpful to the people who are going to take this course.

In this course They have lessons, many quizzes, problem sets and Optional Problem Sets. Quizzes and problem sets have the solutions. But Optional Problem Sets don’t have the solutions. But it’s really good practice to solve the Optional Problem Sets. I decided to share my solutions to those Problem Sets here:

In this course They have lessons, many quizzes, problem sets and Optional Problem Sets. Quizzes and problem sets have the solutions. But Optional Problem Sets don’t have the solutions. But it’s really good practice to solve the Optional Problem Sets. I decided to share my solutions to those Problem Sets here. 

Today I am sharing solutions to two problems:

Problem 1:

Define a procedure weekend which takes a string as its input, and returns the boolean True if it’s ‘Saturday’ or ‘Sunday’ and False otherwise.

Define a procedure weekend which takes a string as its input, and returns the boolean True if it’s ‘Saturday’ or ‘Sunday’ and False otherwise.

Solution:

def weekend(day):

    if day == ‘Saturday’ or day == ‘Sunday’:

        return True

 

    return False

Problem 2:

Define a procedure, stamps, which takes as its input a positive integer in pence and returns the number of 5p, 2p and 1p stamps (p is pence) required to make up that value. The return value should be a tuple of three numbers (that is, your return statement should be followed by the number of 5p, the number of 2p, and the nuber of 1p stamps). Your answer should use as few total stamps as possible by first using as many 5p stamps as possible, then 2 pence stamps and finally 1p stamps as needed to make up the total.

def stamps(n):

    five_div = n % 5

    five_else = n – five_div

    five = five_else / 5

   

    two_div = five_div % 2

    two_else = five_div – two_div

    two = two_else / 2

   

    one = two_div

   

    return (five, two, one)

Hope this will help. I will keep posting solution to more problems in my coming posts.

Leave a Reply

Close Menu