cs homework- 1-3, 5 and midterm notes

Your page rank:

Total word count: 2425
Pages: 9

Calculate the Price

- -
275 words
Looking for Expert Opinion?
Let us have a look at your work and suggest how to improve it!
Get a Consultant

initialize?

assign a value to

Assume that print_error_description is a function that expects one int parameter and returns no value.

Write a statement that invokes the function print_error_description, passing it the value 14.

print_error_description(14)

Write the code to call the function named send_signal. There are no parameters for this function.

send_signal()

Write a statement that reads a word from standard input into firstWord.

input()

Write a statement that reads an integer value from standard input into a variable called val.

val = int(input())

Write a statement that reads a floating point value from standard input into temperature.

temperature = float(input())

Given a variable word that has been assigned a string value, write a string expression that parenthesizes the value of word. So, if word contains "sadly", the value of the expression would be the string "(sadly)"

"(" + word + ")"

Write the definition of a function twice, that receives an int parameter and returns an int that is twice the value of the parameter.

def twice(x): return (2*x)

Write the definition of a function named sum_list that has one parameter, a list whose elements are of type int. The function returns the sum of the elements of the list as an int.

def sum_list(x): return sum(x)

Given the variable c, whose associated value is a str, write an expression that is True if and only if c is not equal to a string consisting of a single blank.

c != " "

Assume that a variable variable , numberOfSides has been initialized. Write a statement that assigns the value True to the variable isQuadrilateral if numberOfSides is exactly 4 and False otherwise.

isQuadrilateral = numberOfSides==4

Given a variable s that is associated with the empty string, write some statements that use a while loop to associate s with a string consisting of exactly 777 asterisks (*) .

s="" x= "*" while len(s)<777: s= x x += "*"

Write a statement that defines plist as the list containing exactly these elements (in order): "spam", "eggs", "vikings" .

plist= ["spam","eggs","vikings"]

Given a variable s that is associated with non-empty empty string, write some statements that use a while loop to associate a variable vowel_count with the number of lower-case vowels ("a","e","i","o","u") in the string .

i=0 vowel_count=0 while i<len(s): c=s[i] if c==’a’ or c==’e’ or c==’i’ or c==’o’ or c==’u’: vowel_count += 1 i += 1

Given the strings s1 and s2 that are of the same length, create a new string consisting of the first character of s1 followed by the first character of s2, followed by the second character of s1, followed by the second character of s2, and so on (in other words the new string should consist of alternating characters of s1 and s2). For example, if s1 contained "hello" and s2 contained "world", then the new string should contain "hweolrllod". Associate the new string with the variable s3.

s3 = "" for i in range(len(s1)) : s3 += s1[i] + s2[i]

Assign True to the variable has_dups if the string s1 has any duplicate character (that is if any character appears more than once) and False otherwise

has_dups = 0 for i in range (0, len(s1)): if (s1.count(s1[i]) > 1): has_dups = True else: has_dups = False

Sort the list, lst (use the list sort method).

lst.sort()

Given that plist has been defined to be a list, write an expression that evaluates to True if 3 is an element of plist.

3 in plist

Given that plist has been defined to be a list of 30 elements, add 5 to its last element.

plist[-1] += 5

Given the lists, lst1 and lst2, create a new sorted list consisting of all the elements of lst1 that also appears in lst2. For example, if lst1 is [4, 3, 2, 6, 2] and lst2 is [1, 2, 4], then the new list would be [2, 2, 4]. Note that duplicate elements in lst1 that appear in lst2 are also duplicated in the new list. Associate the new list with the variable new_list, and don’t forget to sort the new list.

new_list= [] for element in lst1: if element in lst2: new_list.append(element) new_list.sort()

Given the string, s, and the list, lst, associate the variable contains with True if every string in lst appears in s (and False otherwise). Thus, given the string Hello world and the list ["H", "wor", "o w"], contains would be associated with True.

contains = True for i in range(0, len(lst)): if not lst[i] in s: contains = False

Assume that a list of integers named salary_steps that contains exactly five elements has been defined.

Write a statement that changes the value of the last element in the list to 160000.

salary_steps[-1] = 160000

Given a variable named plist that refers to a list, write a statement that adds another element, 5 to the end of the list.

plist.append(5)

Given that play_list has been defined to be a list, write a statement that makes the first 3 elements of play_list be "spam", "eggs" and "vikings" (in that order).

play_list[0:3] = ["spam","eggs","vikings"]

Given that alist has been defined to be a list with at least 4 elements, write a statement that removes its 4th element.

del alist[3]

Given that a refers to a list, write the necessary code to reverse the elements of the list.

a.reverse()

Given that k refers to an int and that play_list has been defined to be a list, write a expression that evaluates to True if the value associated with k is an element of play_list.

k in play_list

Given a list named alist, write an expression that removes the last element of alist.

del(list[-1])

Given that plist1 and plist2 both refer to lists, write a statement that defines plist3 as a new list that is the concatenation of plist1 and plist2. Do not modify plist1 or plist2.

plist2= plist1 + plist2

Given that t has been defined and refers to a tuple write a statement that associates play_list with a list containing the same elements as t.

play_list=list(t)

Assume that plist refers to a list containing exactly five elements. Assume, in addition, that j refers to an int with a value that is between 0 and 3.

Write a statement that associates a new value with the element of the list indexed by j. This new value should be equal to twice the value of the next element of the list (i.e. the element after the element indexed by j.

plist[j]= 2*plist[j+1]

Write the definition of a function, is_reverse, whose two parameters are arrays of integers of equal size. The function returns true if and only if one array is the reverse of the other. ("Reverse" here means same elements but in reverse order.)

def is_reverse(x,y): if y==x[::-1]: return True else: return False

Write a statement that associates t with the empty tuple.

t=()

Given a variable t that is associated with a tuple whose elements are numbers, write some statements that use a while loop to count the number of times the first element of the tuple appears in the rest of the tuple, and associate that number with the variable repeats. Thus if the tuple contains (1,6,5,7,1,3,4,1), then repeats would be assigned the value 2 because after the first "1" there are two more "1"s.

repeats=0 i=1 while i<len(t): if t[0]==t[i]: repeats += 1 i += 1

Given that t refers to a tuple, write a statement that assigns the value of its first element to k.

k=t[0]

An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, …, the distance is 2 while in the sequence 6, 12, 18, 24, …, the distance is 6.

Given the positive integer distance and the integers m and n, create a list consisting of the arithmetic progression between (and including) m and n with a distance of distance (if m &gt; n, the list should be empty.) For example, if distance is 2, m is 5, and n is 12, the list would be [5, 7, 9, 11].

Associate the list with the variable arith_prog.

arith_prog = list(range(m,n+1,distance))

A triangular number is a number that is the sum of the integers from 1 to some integer n. Thus 1 is a triangular number because it’s the sum of the integers from 1 to 1; 6 is a triangular number because it’s 1+2+3=6.

Given the non-negative integer n, create a list of the first n triangular numbers. Thus is n was 5, the list would be: [1, 3, 6, 10, 15]. Associate the list with the variable triangulars.

Sum = 0 triangulars = [] for x in range(1,1+n): Sum += x triangulars.append(Sum)

Write an expression that evaluates to the value of the first element of the tuple that t refers to.

t[0]

Given that t has been defined and refers to a tuple write some statements that associate with t a new tuple containing the same elements as the original but in sorted order.

t=list(t) t=sorted(t) t=tuple(t)

Write a statement that associates t with a tuple that contains the following elements: 42, 56, 7 .

t=(42,56,7)

Given that play_list has been defined and refers to a list, write a statement that associates t with a tuple containing the same elements as play_list.

t=tuple(play_list)

Create a list of the odd numbers between m and n (assume m is odd; also, include n– if its odd– in the list), and associate it with the variable odds.

odds= list(range(m,n+1,2))

Create a list of the odd numbers between 1 and n (include 1 as well as n — if it’s odd– in the list). Associate the list with the variable odds.

odds= list(range(1,n+1,2))

"ABC".join(["A","B","C"])

= AABCBABCC

x=0
i=1
while (i*i)&lt;=49:
if (i%2) == 1:
x += 1
i =i + 1

what is final value of x?

4

x=0
i=1
while (i*i)&lt;=49:
if (i%2) == 1:
x += 1
i =i + 1

how does this code work

goes through if statement first if it satisfies that, adds 1 to x and will add 1 to i if it doesn’t satisfy that, it will just ass 1 to i and keep going

s = "MEWTWO"
x=""
for i in range (0, len(s)):
if (i&gt;1) and (i&lt;3):
x += s[i:i+3]
what is value of x

"WTW"

s= "SQUIRTLE"
x=""
for i in range (0, len(s)):
if (i&gt;4) and (i&lt;7):
x += s[i:i+2]
what is final value of x?

"TLLE"

s= "A,E,I,O,U".split(",")
s=s[0:3]
s=s.sort()
value of s

none

def fun(a,b):
for i in range(a,b):
if (i%3)==0:
return i
return a==b

a=4
b=6
print fun(a,b)

False

does range() include rightmost digit

no

x= ["tick","tock"]
x[0]=x.reverse()
x=x[-2]
type of x after program is run?

None type because x.reverse() returns a none

x=["tick","tock"]
s[0]=len(list(x[-1]))
x=x[-2]
what is the type of x after the program is run

integer

x=0
i=1
while (i*i)&lt;=36:
if ((i*i)%2)==0:
x += 1
i=i+1
final value of x

x=3

s="GABE&amp;TYCHO"
x=s[3:6]
what is value of x after program is executed?

"E&T"

how to put more than one pair of quotes in python

start with single quotes first

x=["tick","tock"]
x[-1]=list(x[0])
x=x[1],x[0]
what is type of x after program is run?

tuple

how to differentiate between tuple and list

tuple has no brackets, immutable

x=["tick","tock"]
x[0]=(len(list(x[-1])),x[1])
x=x[1]
type of x?

string

def fun(a,b):
return a-b
x=0
for i in range(2,5):
x=x+fun(i,x)
print(x)

3

str["A","B"]

doesn’t return anything

list("ABC")

["A","B","C"]

list("ABC").append("D")

doesn’t exist

def fun(a,b):
return a-b
x=0
for i in range (-1,3):
x=x+fun(i,x)
print x

2

a=list("ACCIO")
a.reverse()
a[1],a[2]=a[2],a[3]
x=""
for e in a:
x=x+e
final value of x?

=["A","C","C","I","O"] =["O","I","C","C","A"] "I","C","C"- tuple = "OCCCA"

len("ABCD"[1:3])

2

s="CHARIZARD"
x=""
for i in range(0,len(s)):
if (i&gt;3) and (i&lt;6):
x+=s[i:i+2]

"IZZA"

"+".join("ABABABA".split("A"))

("ABABABA".split("A")) = " B B B " "+".join("ABABABA".split("A")) = "+B+B+B+"

.split()

will replace string value inside split with a blank space

def fun(a,b):
if a&gt;b and a!=4:
return b==5
else:
return a==3
a=5
b=4
print fun(a,b)

False

a=list("REDUCIO")
a.sort()
a[0],a[1]=a[-2],a[-1]
x=""
for e in a:
x=x+e

=["R","E","D","U","C","I","O"] =["C","D","E","I","O","R","U"] ="C","E"="I","O" ="RUEIORU"

def fun(a,b):
return a-b
x=0
for i in range(1,4):
x=x+fun(i,x)

3

what does None Type stand for

null

what does None mean

means that x has no value

x[0]=x.reverse()?

returns a None

Share This
Flashcard

More flashcards like this

NCLEX 10000 Integumentary Disorders

When assessing a client with partial-thickness burns over 60% of the body, which finding should the nurse report immediately? a) ...

Read more

NCLEX 300-NEURO

A client with amyotrophic lateral sclerosis (ALS) tells the nurse, "Sometimes I feel so frustrated. I can’t do anything without ...

Read more

NASM Flashcards

Which of the following is the process of getting oxygen from the environment to the tissues of the body? Diffusion ...

Read more

Unfinished tasks keep piling up?

Let us complete them for you. Quickly and professionally.

Check Price

Successful message
sending