Question

In: Computer Science

1)Given a list L1, create a list L2 that contains all but the last element of...

1)Given a list L1, create a list L2 that contains all but the last element of L1 (e.g. if L1 is ["a","z",True], L2 should be equal to ["a","z"]

2)Given a string s, assign a variable has_z that is True if s contains the letter "z" anywhere within it.

3)Write Python code that calculates the mean of a given list of numbers x and saves the result in a variable m.

4)Given two numeric variables, x and y, assign the maximum of x and y to a variable m.

5)given a string s, write Python code that sets a boolean variable has_two_caps to True if the string contains exactly two capital letters (use the .isupper() string method to test individual letters) and False otherwise.

6)Given two variables x and y containing numerical values and a boolean variable use_x, assign a third variable z the value of x if use_x is True; assign it the value of y if use_x is False.

7)Given a list of logical values x, set all_x to True if all of the values in x are True, and False otherwise

IN PYTHON!!

Solutions

Expert Solution

def part_one(l):
   return l[:len(l)-1]

def part_two(s):
   return ('z' in s) or ('Z' in s)

def part_three(l):
   return sum(l)/len(l)

def part_four(x,y):
   return x if x > y else y

def part_five(s):
   count = 0
   for x in s:
       if count >= 2:
           return True
       elif x.isupper():
           count = count + 1
   # for last digit
   if count >= 2:
       return True
   else:
       return False

def part_six(x,y,use_x):
   return x if use_x else y

def main():
   print("Part One")
   l = eval(input("Enter your list: "))
   print(part_one(l))

   print("\nPart Two")
   s = input("Enter s: ")
   has_z = part_two(s)
   print("has_z: ",has_z)

   print("\nPart Three")
   l = eval(input("Enter list for sum: "))
   m = part_three(l)
   print("m: ",m)

   print("\nPart Four")
   x = float(input("Enter x : "))
   y = float(input("Enter y : "))
   m = part_four(x,y)
   print("m: ",m)

   print("\nPart Five")
   s = input("Enter s: ")
   has_two_caps = part_five(s)
   print("has_z: ",has_two_caps)

   print("\nPart Six")
   x = float(input("Enter x: "))
   y = float(input("Enter y: "))
   use_x = eval(input("Enter use_x: "))
   z = part_six(x,y,use_x)
   print("z: ",z)

main()


Related Solutions

The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list...
The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list containing elements in both L1 and L2 only. Given two sorted lists L1 and L2, write a function, called intersection, to compute L1 ∩ L2 using only the basic list operations. The intersection function is defined as follows template list intersection( const list & L1, const list & L2); C++
1) Equations for two lines L1 and L2 are given. Find the angle between L1 and...
1) Equations for two lines L1 and L2 are given. Find the angle between L1 and L2. L1: ? = 7 + 2?, ? = 8 − 4?, ? = −9 + ? L2: ? = −8 − ?, ? = 3 − 3?, ? = 4 + 3? 2) Find polar form of complex number z : ?) ? = 4√3 − 4? ?) ? = 2√3 − 2i
Given two sorted lists L1 and L2, write a procedure to compute L1∪L2 using only the...
Given two sorted lists L1 and L2, write a procedure to compute L1∪L2 using only the basic list operations. Pseudo-code is acceptable.
1. Write a Racket function (set-equal? L1 L2) that tests whether L1 and L2 are equal....
1. Write a Racket function (set-equal? L1 L2) that tests whether L1 and L2 are equal. Two sets are equal if they contain exactly the same members, ignoring ordering (or in other words, two sets are equal if they are a subset of each other). For example (set-equal? '(1 (2 3)) '((3 2) 1)) ---> #t (set-equal? '(1 2 3) '((3 2)1)) ---> #f (set-equal? '(1 2 3) '((1 2 3))) ---> #f 2. Two common operations on sets are...
create a function to merge and concatenate two linked list l1 =[1,2,4] and l2=[1,3,4]. dont modify...
create a function to merge and concatenate two linked list l1 =[1,2,4] and l2=[1,3,4]. dont modify l2 (the nodes inserted in l1 should be copies of l2 class Node(object): def __init__(self, data): self.data = data self.next = None    class LinkedList(object): def print_list(self): cur_node = self.head while cur_node: print(cur_node.data) cur_node = cur_node.next def __init__(self): self.head = None;    def append(self, data): new_node = Node(data) if self.head is None: self.head = new_node return last_node = self.head while last_node.next: last_node = last_node.next...
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩...
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩ L2 using only the basic STL list operations. What is the running time of your algorithm?
Recall that given a line L1 in the plane with slope m, a second line L2...
Recall that given a line L1 in the plane with slope m, a second line L2 is perpendicular to L1 if and only if the slope of L2 is − 1 m . Now consider the function h(x) = x^2 + 3/2x + 3. Among all of the lines tangent to the graph of h(x), there is exactly one which is perpendicular to the line given by y = −2x + 7. Write the equation of that line tangent to...
Create a F# Function. Function Signature: zip L1 L2 The function zip combines the elements of...
Create a F# Function. Function Signature: zip L1 L2 The function zip combines the elements of two lists pairwise, resulting in a list of tuples, where the first tuple in the list contains the first element of L1 as the first element, and the first element of L2 as the second element // // zip L1 L2 // // Zip two lists // // Returns list of tuples // // Examples: // zip [] [] => [] // zip [1]...
(a) Find the cosine of the angle between the lines L1 and L2 whose vector equations are given below:
(a) Find the cosine of the angle between the lines L1 and L2 whose vector equations are given below: L1 : ~r1(t) = [1, 1, 1] + t[1, 2, 3] L2 : ~r2(t) = [1, 1, 1] + t[−1, 4, 2]. (b) Find the equation of the plane that contains both L1 and L2.
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT