Subject Computer Science Python Programming

Question

Q1 :   
n = 10
nx = range(2,4)
s = "hi"
sx = ["down", "town"]
state which of the following lines are correct Python and which are not. If they are correct, write the output they produce.
printNow( n + n )
printNow( nx + nx )
printNow( 2*n + 1 )
printNow( 2*nx + nx )
printNow( s + s )
printNow( s + "clown" )
printNow( sx + sx )
printNow( sx[1] + sx[1] )
printNow( sx[2] )
printNow( n[0] )
printNow( n + [1] )
printNow( nx + [1] )

Q2:
Searching is the process of looking for a particular value in a collection -- a list in our case. For example, a program that maintains a membership list for a club might need to look up information for a particular member -- this involves some sort of search process.
The first type of searching tasks is finding whether something is in a list. For examples: 45 is in list [50,24,19,30,99,45,54,88,92,36] while 66 isn't in the same list.
The second type of searching task is finding where something is in a list. For example: 45 is in slot 5 of the list [50,24,19,30,99,45,54,88,92,36] while the slot of 66 is -1, which means there isn't the item in this list.
Please write a python function to finish these two search functions.
Here is the algorithm for searching "whether":
1. assume you have a list [50,24,19,30,99,45,54,88,92,36]
2. start at the top of the list, scanning downward, comparing each number to 45? If the number is 45, then searching stops and print out "Find 45"; otherwise, print "Didn't find it."
2.1 while the index is less than the length of the list and the program hasn't find the number, do the following things:
2.1.1 Compare the current element with the target number.
2.1.2 Increase the index number.

Here is the algorithm for searching "where":
1. assume you have a list [50,24,19,30,99,45,54,88,92,36]
2. start at the top of the list, scanning downward, comparing each number to 45? If the number is 45, then searching stops and print out the index of 45; otherwise, print "-1"

Q3:
Write a function to calculate the sum of a list with a length of 10, and the elements of this list are the random integers from 1 to 10 (inclusive). Also report how many 1s are in the list. For example, if there are two elements of 1, then report “there are two 1s.” Here are the pseudo codes:
1. define an empty list called li
2. define a counter called counter to record how many 1 in the list.
3. repeat the following things 10 times
3.1 generate a random integer between 1 to 10 (inclusive)
3.2 add this integer into the list li
3.3 if the integer is equal to 1, then counter increases 1.
4. print out the list
5. print out how many 1s in the list.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

# Q1
n = 10
nx = range(2,4)
s = "hi"
sx = ["down", "town"]

printNow(n+n)
# 20

printNow(nx+nx)
# [2,3,2,3]

printNow(2*n + 1)
# 21

printNow( 2*nx + nx )
# [2, 3, 2, 3, 2, 3]...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor

Related Homework Solutions

Get help from a qualified tutor
Live Chats