) Write Python programs for the following %u2013 include a screenshot of your ou
ID: 3536337 • Letter: #
Question
) Write Python programs for the following %u2013 include a screenshot of your output along with your code.
(6pts) Write a function called PrimesList that takes as input a positive integer number n, and returns a list with all the prime numbers that are less or equal to n in ascending order.
For example:
>>>print PrimesList(5)
[2,3,5]
(6pts) Write a function Split that takes a list and a number n as its arguments. The function should split the list into two parts. The length of the first part would be of size n.
For Example:
>>>print Split([1,2,3,4,5,6],3)
[1,2,3]
(6pts) Write a Python class called Employee that holds name, ss#, hoursWorkedinWeek, and salary. Add methods to calculate pay based on hours and salary (give overtime (1.5) if hours for the week are more than 40), to print information about an employee (lookup by ss#), to change information for an employee, and to find the employee who worked the most hours in the week. Use your class to create a list of three employees and test each of your methods.
(6pts) Write a class that implements a struct. In your struct store Pet information such as name, type of animal, and age. Write a function that can access a pet in a list of 5 structs by name and print their type and age. Show that your function works by calling it.
Explanation / Answer
here is the code and the output is on the next post
Ans1:
def PrimesList(n):
primes=[];
if n<2:
return primes;
primes.append(2);
if n==2:
return primes;
for i in range(3,n):
isprime=1;
for j in range(2,i/2+1):
if i%j==0 :
isprime=0;
if isprime==1:
primes.append(i);
return primes;
print " prime numbers less than 100 is"
print PrimesList(100);
Ans2:
def split(l,n):
return l[0:0+n];
list=[1,2,3,4,5,6]
print split(list,3);
Ans3:
class Employee:
def __init__(self, name,ss,hoursWorkedinWeek,salary):
self.name=name;
self.ss=ss;
self.hoursWorkedinWeek=hoursWorkedinWeek;
self.salary=salary;
def calculatePay(self):
if self.hoursWorkedinWeek>40:
return 40*self.salary+(self.hoursWorkedinWeek-40)*self.salary;
else :
return self.hoursWorkedinWeek*self.salary;
def displayEmployee(self):
print "Name : ", self.name, ", ss: ", self.ss, ", hoursWorkedinWeek: ",self.hoursWorkedinWeek,", salary: ",self.salary, "totalpay: ",self.calculatePay();
def setSalary(self,salary):
self.salary=salary;
def setName(self,name):
self.name=name;
def setHoursWorkedinWeek(self,hoursWorkedinWeek):
self.hoursWorkedinWeek=hoursWorkedinWeek;
def getHoursWorkedinWeek(self):
return self.hoursWorkedinWeek;
def calculateEmployeeWorkedMost(self,l):
e=l[0];
for Emp in l:
if(Emp.hoursWorkedinWeek>e.hoursWorkedinWeek):
e=Emp;
return e;
l=[Employee("ronaldo",3,36,100),Employee("messi",5,42,100),Employee("field",6,37,200)];
print l[0].displayEmployee();
print l[1].displayEmployee();
print l[2].displayEmployee();
print "employee who worked the most hours in the week is : "
l[0].calculateEmployeeWorkedMost(l).displayEmployee();
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.