Skip to content

Commit

Permalink
added more questions
Browse files Browse the repository at this point in the history
  • Loading branch information
xcao21 committed Jun 15, 2012
1 parent dc8778e commit e379474
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 8 deletions.
263 changes: 255 additions & 8 deletions 100+ Python challenging programming exercises.txt
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,8 @@ def printValue(n):
printValue(3)

#----------------------------------------#
2.10

Question:
Define a function that can receive two integral numbers in string form and compute their sum and then print it in console.

Expand All @@ -781,6 +783,9 @@ printValue("3","4") #7


#----------------------------------------#
2.10


Question:
Define a function that can accept two strings as input and concatenate them and then print it in console.

Expand All @@ -795,6 +800,9 @@ def printValue(s1,s2):
printValue("3","4") #34

#----------------------------------------#
2.10


Question:
Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print al l strings line by line.

Expand All @@ -820,6 +828,8 @@ printValue("one","three")


#----------------------------------------#
2.10

Question:
Define a function that can accept an integer number as input and print the "It is an even number" if the number is even, otherwise print "It is an odd number".

Expand All @@ -839,22 +849,259 @@ checkValue(7)


#----------------------------------------#
2.10

Question:
Define a function that can accept an integer number as input and print the "It is an even number" if the number is even, otherwise print "It is an odd number".
Define a function which can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are square of keys.

Hints:

Use % operator to check if a number is even or odd.
Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.

Solution
def checkValue(n):
if n%2 == 0:
print "It is an even number"
else:
print "It is an odd number"
def printDict():
d=dict()
d[1]=1
d[2]=2**2
d[3]=3**2
print d


checkValue(7)
printDict()





#----------------------------------------#
2.10

Question:
Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.

Hints:

Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
Use range() for loops.

Solution
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
print d


printDict()


#----------------------------------------#
2.10

Question:
Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the values only.

Hints:

Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
Use range() for loops.
Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.

Solution
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
for (k,v) in d.items():
print v


printDict()

#----------------------------------------#
2.10

Question:
Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.

Hints:

Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
Use range() for loops.
Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.

Solution
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
for k in d.keys():
print k


printDict()


#----------------------------------------#
2.10

Question:
Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).

Hints:

Use ** operator to get power of a number.
Use range() for loops.
Use list.append() to add values into a list.

Solution
def printList():
li=list()
for i in range(1,21):
li.append(i**2)
print li


printList()

#----------------------------------------#
2.10

Question:
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.

Hints:

Use ** operator to get power of a number.
Use range() for loops.
Use list.append() to add values into a list.
Use [n1:n2] to slice a list

Solution
def printList():
li=list()
for i in range(1,21):
li.append(i**2)
print li[:5]


printList()


#----------------------------------------#
2.10

Question:
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.

Hints:

Use ** operator to get power of a number.
Use range() for loops.
Use list.append() to add values into a list.
Use [n1:n2] to slice a list

Solution
def printList():
li=list()
for i in range(1,21):
li.append(i**2)
print li[-5:]


printList()


#----------------------------------------#
2.10

Question:
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list.

Hints:

Use ** operator to get power of a number.
Use range() for loops.
Use list.append() to add values into a list.
Use [n1:n2] to slice a list

Solution
def printList():
li=list()
for i in range(1,21):
li.append(i**2)
print li[5:]


printList()


#----------------------------------------#
2.10

Question:
Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included).

Hints:

Use ** operator to get power of a number.
Use range() for loops.
Use list.append() to add values into a list.
Use tuple() to get a tuple from a list.

Solution
def printTuple():
li=list()
for i in range(1,21):
li.append(i**2)
print tuple(li)

printTuple()



#----------------------------------------#
2.10

Question:
With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line.

Hints:

Use [n1:n2] notation to get a slice from a tuple.

Solution
tp=(1,2,3,4,5,6,7,8,9,10)
tp1=tp[:5]
tp2=tp[5:]
print tp1
print tp2


#----------------------------------------#
2.10

Question:
Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10).

Hints:

Use "for" to iterate the tuple
Use tuple() to generate a tuple from a list.

Solution
tp=(1,2,3,4,5,6,7,8,9,10)
li=list()
for i in tp:
if tp[i]%2==0:
li.append(tp[i])

tp2=tuple(li)
print tp2



Expand Down
Binary file added python contents.docx
Binary file not shown.

0 comments on commit e379474

Please sign in to comment.