CIS Exam 4 Coding

Your page rank:

Total word count: 3164
Pages: 12

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

Use the variables k and total to write a while loop that computes the sum of the squares of the first 50 counting numbers, and associates that value with total. Thus your code should associate 1<b>1 + 2</b>2 + 3<b>3 +… + 49</b>49 + 50*50 with total. Use no variables other than k and total.

total = 0 k = 50 while k > 0: total += k * k k -= 1

Given that n refers to a positive int use a while loop to compute the sum of the cubes of the first n counting numbers, and associate this value with total. Use no variables other than n, k, and total.

total = 0 k = 0 while k <= n: total += k k k k += 1

In this exercise, use the following variables: i,lo, hi, and result. Assume that lo and hi each are associated with an int and that result refers to 0.

Write a while loop that adds the integers from lo up through hi (inclusive), and associates the sum with result.

Your code should not change the values associated with lo and hi. Also, just use these variables: i,lo, hi, and result.

i = lo while i <= hi: result += i i += 1

Assume there is a variable, h already associated with a positive integer value. Write the code necessary to compute the sum of the perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3<b>3, 4</b>4, 5<b>5, 6</b>6 respectively).) Associate the sum you compute with the variable q. For example, if h is 19, you would assign 30 to q because the perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16 and 30==1+4+9+16.

q = 0 i = 1 while (i * i < h) : q += i * i i += 1

Assume there is a variable, h already associated with a positive integer value. Write the code necessary to count the number of perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3<b>3, 4</b>4, 5<b>5, 6</b>6 respectively).) Assign the sum you compute to a variable q For example, if h is 19, you would assign 4 to q because there are perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16.

q = 0 i = 1 while i * i < h : q += 1 i += 1

Assume there are two variables, k and m, each already associated with a positive integer value and further assume that k’s value is smaller than m’s. Write the code necessary to compute the number of perfect squares between k and m. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3<b>3, 4</b>4, 5<b>5, 6</b>6 respectively).) Associate the number you compute with the variable q. For example, if k and m had the values 10 and 40 respectively, you would assign 3 to q because between 10 and 40 there are these perfect squares: 16, 25, and 36,.

i = 1 q = 0 while i * i < k : i += 1 while i * i <= m : q += 1 i += 1

Use two variables k and total to write a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus, your code should put 1<b>1 + 2</b>2 + 3<b>3 +… + 49</b>49 + 50*50 into total. Use no variables other than k and total.

total = 0 for k in range(51): total += k*k

Given a variable n refers to a positive int value, use two additional variables, k and total to write a for loop to compute the sum of the cubes of the first n counting numbers, and store this value in total. Thus your code should put 1<b>1</b>1 + 2<b>2</b>2 + 3<b>3</b>3 +… + n<b>n</b>n into total. Use no variables other than n, k, and total.

total = 0 for k in range(n + 1): total += k k k

Associate the average of the numbers from 1 to n (where n is a positive integer value) with the variable avg.

avg = (1 + n) / 2

Assume there is a variable, h already associated with a positive integer value. Write the code necessary to compute the sum of the first h perfect squares, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3<b>3, 4</b>4, 5<b>5, 6</b>6 respectively).) Associate the sum you compute with the variable q. For example, if h is 4, you would assign 30 to q because the first 4 perfect squares (starting with 1) are: 1, 4, 9, 16 and 30==1+4+9+16.

q = 0 for i in range(1, h + 1) : q += i * i

Associate True with the variable is_ascending if the list numbers is in ascending order (that is, if each element of the list is greater than or equal to the previous element in the list). Otherwise, associate False with is_ascending

for i in range(2, len(numbers)) : if numbers[i] < numbers[i-1] : is_ascending = False break else : is_ascending = True

Given a positive integer n, assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)

for i in range(2, n) : if n % i == 0 : is_prime = False break else : is_prime = True

Given the positive integer distance and the positive integer n, associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance. For example, if distance is 2 and n is 10, then sum would be associated with 25 because 1+3+5+7+9 = 25.

sum = 0 for i in range(1, n+1, distance) : sum += i

Write a statement that defines plist to be the empty list.

plist = []

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

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

Write statement that defines plist to be a list of the following ten elements: 10, 20, 30, …, 100 in that order.

plist = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

Create a list named tax_rates, consisting of the following five elements: 0.10, 0.15, 0.21, 0.28, 0.31, in that order.

tax_rates = [0.10, 0.15, 0.21, 0.28, 0.31]

Write a statement that defines the variable denominations, and associates it with a list consisting of the following six elements: 1, 5, 10, 25, 50, 100, in that order.

denominations = [1, 5, 10, 25, 50, 100]

Associate True with the variable has_dups if the list list1 has any duplicate elements (that is if any element appears more than once), and False otherwise.

for i in range(len(list1)- 1): if list1[i] in list1[i+1:] : has_dups = True break else : has_dups = False has_dups = len(list1) != len(set(e for e in list1))

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

plist[-1] += 5

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 plist, that refers to a non-empty list, write an expression that refers to the first element of the list.

plist[0]

Given a variable plist, that refers to a list with 34 elements, write an expression that refers to the last element of the list.

plist[-1] plist[33]

Assume that the variable plist has been defined and refers to a list. Write a statement that assigns the next to last element of the list to x.

x = plist[-2]

Given that a variable named plist has been defined and refers to a non-empty list, write a statement that associates its first element with 3.

plist[0] = 3

Assume that salary_steps refers to a non-empty list, write a statement that assigns the value 30000 to the first element of this list.

salary_steps[0] = 30000

Assume that plist has been defined and is associated with a non-empty list. Write a statement that increases the first element of this list by 10.

plist[0] += 10

Assume that a variable named plist refers to a list with 12 elements, each of which is an int. Assume that the variable k refers to a value between 0 and 6. Write a statement that assigns 15 to the list element whose index is k.

plist[k] = 15

Given that plist refers to a non-empty list ,write a statement that assigns the int -1 to the last element of the list.

plist[-1] = -1

Assume that plist is associated with a list that has 12 elements. Assume further that k refers to an int between 2 and 8. Assign 9 to the element just after the element in plist whose index is k .

plist[k+1] = 9

Assume that a variable named plist has been defined and is associated with a list that consists of 12 elements. Assume further that k refers to an int between 2 and 8. Assign 22 to the element just before the element in plist whose index is k .

plist[k-1] = 22

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] = plist[j+1] * 2

Assume that play_list refers to a non-empty list, and that all its elements refer to values of type int. Write a statement that associates a new value with the first element of the list. The new value should be equal to twice the value of the last element of the list.

play_list[0] = play_list[-1] * 2

Given that plist1 and plist2 both refer to lists, write an expression that evaluates to a list that is the concatenation of plist1 and plist2. Do not modify plist1 or plist2.

plist1 + plist2

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.

plist3 = plist1 + plist2

Given that play_list has been defined to be a list, write an expression that evaluates to a new list containing the elements at index 0 through index 4 play_list. Do not modify play_list.

play_list[0:5]

Given that k and j each refer to a non-negative int and that play_list has been defined to be a list with at least j+1 elements, write an expression that evaluates to a new list containing all the elements from the one at index k through the one at index j of list play_list . Do not modify play_list .

play_list[ k : j + 1]

Given that k and j each refer to a non-negative int and that play_list has been defined to be a list with at least j elements, write an expression that evaluates to a new list containing all the elements from the one at index k through the one at index j-1 of list play_list . Do not modify play_list .

play_list[ k : j ]

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 L1 and L2 both refer to lists, write a statement that replaces the elements in L1 from index 5 through (and including) index 8 with all the elements of L2.

L1[5:9] = L2

Given that worst_offenders has been defined as a list with at least 6 elements, write a statement that defines lesser_offenders to be a new list that contains all the elements from index 5 of worst_offenders and beyond. Do not modify worst_offenders.

lesser_offenders = worst_offenders[ 5 : ]

Given that k refers to an int that is non-negative and that plist1 has been defined to be a list with at least k+1 elements, write a statement that defines plist2 to be a new list that contains all the elements from index k of plist1 and beyond. Do not modify plist1.

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 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 variable current_members that refers to a list, and

-a variable member_id that has been defined.

Write some code that assigns True to a variable is_a_member if the value associated with member_id can be found in the list associated with current_members, but that otherwise assigns False to is_a_member. Use only current_members, member_id, and is_a_member.

is_a_member = member_id in current_members

You are given a variable zipcode_list that refers to a list.

Write some code that assigns True to a variable duplicates if there are two adjacent elements in the list that have the same value, but that otherwise assigns False to duplicates otherwise. In order to accomplish this, you may, if you wish, use one other variable, k.

Use only k, zipcode_list, and duplicates.

duplicates = False k = 0 while k < len(zipcode_list)-1 and not duplicates: duplicates = zipcode_list[k] == zipcode_list[k+1] k += 1

You are given a variable named zipcode_list that has been defined and refers to a list of postal codes.

Write some code that assigns True to duplicates if any two elements in the list have the same value, but that otherwise assigns False to duplicates. You may, if you wish, use two other variables, j and k.

Use only j, k, zipcode_list, and duplicates.

duplicates = False j = 0 while j < len(zipcode_list)-1 and not duplicates: k = j + 1 while k < len(zipcode_list) and not duplicates: if zipcode_list[k] == zipcode_list[j]: duplicates = True k += 1 j += 1

Given a variable temps that refers to a list, all of whose elements refer to values of type float, representing temperature data, compute the average temperature and assign it to a variable named avg_temp. Besides temps and avg_temp, you may use two other variables — k and total.

k = 0 total = 0 for i in range(0, len(temps)): total = total + temps[i] k = k + 1 avg_temp = total / k

Given:

-a variable named incompletes that refers to a list of student ids, and

-a variable student_id

Write some code that counts the number of times the value associated with student_id appears in the list associated with incompletes and assigns this value to number_of_incompletes. You may use, if you wish, an additional variable, k.

You may use only k, incompletes, student_id, and number_of_incompletes.

number_of_incompletes = 0 for k in incompletes: if student_id == k: number_of_incompletes += 1

A list named parking_tickets has been defined to be the number of parking tickets given out by the city police each day since the beginning of the current year. (Thus, the first element of the list contains the number of tickets given on January 1; the last element contains the number of tickets given today.)

Write some code that associates most_tickets with the largest value found in parking_tickets. You may, if you wish, use one additional variable, k.

most_tickets = parking_tickets[0] for k in parking_tickets: if k > most_tickets: most_tickets = k

Associate the sum of the non-negative values in the list numbers with the variable sum.

sum = 0 for i in numbers : if i >= 0 : sum += i

We informally define the term corresponding element as follows:

-The first element and the last element of a list are corresponding elements.

-Similarly, the second element and the element just before the last element are corresponding elements.

-The third element and the element just before the element just before the last element are corresponding elements — and so on.

Given that the variable a is associated with a list, write an expression for the corresponding element of a[i].

a[len(a) – i – 1]

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

a.reverse()

Given a list named play_list, write an expression whose value is the length of play_list.

len(play_list)

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

alist.pop()

Given a variable alist that refers to a list, remove the list’s last element and associates its value with a variable k.

k = alist.pop()

Given that play_list has been defined to be a list, write a statement that sorts the list.

play_list.sort()

Reverse the list associated with the variable words.

words.reverse()

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

lst.sort()

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 i in lst1 : if i in lst2 : new_list.append(i) new_list.sort()

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 k refers to a non-negative int and that alist has been defined to be a list with at least k+1 elements, write a statement that removes the element at index k.

del alist[k]

Given that alist has been defined to be a non-empty list (that is with at least one element), write a statement that removes its first element.

del alist[0]

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 the lists list1 and list2 that are of the same length, create a new list consisting of the last element of list1 followed by the last element of list2, followed by the second to last element of list1, followed by the second to last element of list2, and so on (in other words the new list should consist of alternating elements of the reverse of list1 and list2). For example, if list1 contained [1, 2, 3] and list2 contained [4, 5, 6], then the new list should contain [3, 6, 2, 5, 1, 4]. Associate the new list with the variable list3.

list3 = [] for i in range(len(list1)-1, -1, -1) : list3.append(list1[i]) list3.append(list2[i])

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