Write a function that generates a list of all prime numbers

Assignment Help Python Programming
Reference no: EM132369322

Attempt the following tutorial questions:

Part 1:

1-) You are given a string that holds a message encoded with a Caesar cipher of unknown distance. Suggest an algorithm for cracking this message.

2-) A bit shift is a procedure that takes a binary string and moves the digits either left or right, wrapping digits around as required; e.g. a left shift of 1 applied to 10011 gives 00111, and a left-shift of 2 gives 01110. Write a code segment that takes a string s, an integer k, and a Boolean b, and that prints s shifted by k, left if b if True, and right otherwise.

3-) Write a function which can take an integer N as an input and display all prime numbers between 0 and N. In addition, the function is expected to return the total number of prime numbers found.

4-) Write a Python function that, given the path to a directory, lists the files in the directory (Hint: The function is actually quite simple, but it will require a function that you will find in the os module).

Part 2:
5-) Show the string that would result from each of the following string formatting operations. If the operation is not legal, explain why.

(a) "Looks like {1} and {0} for breakfast".format("eggs", "spam")

(b) "There is {0} {1} {2} {3}".format(1,"spam", 4, "you")

(c) "Hello {0}".format("Susan", "Computewell")

(d) "{0:0.2f} {0:0.2f}".format(2.3, 2.3468)

(e) "{7.5f} {7.5f}".format(2.3, 2.3468)

(f) "Time left {0:02}:{1:05.2f}".format(1, 37.374)

(g) "{1:3}".format("14")

6-) i-) Write a function that generates a list of all prime numbers upto N. The function should take N as parameter and return the list.
ii-) Write a function that returns a list of the first N prime numbers.

Hint for question 6:You can skip testing even numbers as no even number besides 2 is a prime number. Also notice that your function must "return a list".

7-) Define a Python function for the function header: intersect(r1, x1, y1, r2, x2, y2), where r1 and r2 are the radii of the two circles whose corresponding centres are at (x1, y1) and (x2, y2). The function returns True if the cicles intersect; False otherwise. Hint: Calculate the distance between the center of the circles.

8-) Write a function that takes a list as a parameter and swaps pairs of consecutive values in the list i.e. index 0 and 1, 2 and 3, 4 and 5 and so on. If there are odd number of elements in the list, the last element remains the same.

9-) Write a code segment that opens a file for input and prints the number of 4-letter words in the file.

10-) Write a script that prompts the user for the names of two text files, inputs the contents of the first file, and writes them to the second file.
11-) Write a function that reads a text file, encrypts the text with public key encryption and writes the encrypted text to another file.
Hint: Use the public, private key pairs provided in this PDF document by DrAjmalMian.

12-) Write another function that will read the encrypted file, decypher it and print the plain text on the screen.

Part 3:

13-) As part of the solution to the SEND + MORE = MONEY cryptarithmetic problem, I suggested that you need a function to ensure that each solution only involves unique digits. Based on only using lists here is one implementation of that function:
defall_different(S, E, N, D, M, O, R, Y) :
digitlist = [S, E, N, D, M, O, R, Y]
digitlist.sort()
for i in range(len(digitlist) - 1) :
if digitlist[i] == digitlist[i+1] :
return False
return True
You will learn in your Level 2 units that sorting, which dominates this function, takes O(NlogN) time. That is, the run-time for this kind of program will rise proportional to NlogN, where N is the size of the input. What you are to do now is recode the function using a dictionary in which you record the number of times each digit has been seen. This implementation will have O(N) run time, which grows more slowly as N increases, so is better.

14-) i-) Write a function that reads the marks.txt file and prints the names of the students who obtained maximum marks in each subject. e.g. "Tom Hanks got the highest marks of 90 in CITS1401".
ii-) write a function that will calculate the average marks obtained in each subject in the above file and print them along with the subject name.
iii-) if passing marks in each subject are 50, write a function that will print the number of students failed in each subject.
v-) write a function that will calculate and print the overall total marks of each student along with the student's name.

15-) Write a function that can return the sum of the digits of the number N which is provided as an input.

16-) Write a function which takes two inputs: list of numbers and a number, and returns list of lists containing three numbers from the input lists such that the sum of three numbers equal to a given number. For instance, myfunction([1, 0, -1, 0, -2, 2], 0) returns [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]].

17-) Write a function which takes any positive integer n, if n is even, divide it by 2 to get n/2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Store the results in a list and repeat the process until it reaches 1. For instance, myfunction(12) returns [12, 6.0, 3.0, 10.0, 5.0, 16.0, 8.0, 4.0, 2.0, 1.0].

18-) Write a function to take a positive integer as an input and add the digits of the input repeatedly until the result has a single digit. For instance, myfunction(48) returns 3.

19-) Write a function that can display a triangle of asterisk (*). The height of the triangle will be provided as an input.

*
***
*****
*******
*********

Reference no: EM132369322

Questions Cloud

Create a word frequency histogram : Create a word frequency histogram from the list of keywords that state which one appears most in which data source - You should store the data in a format
Define what are the main advantages of using multiple models : Chapter 5, Figure 5.1 is an illustration of the future state of practice of systems modeling and simulation. With this mind, the adoption of recent, current.
Describe the social work practice skills you developed : Describe whether you met or did not meet the goals/objectives in your agency learning agreement and include the results from your evaluation.
Calculate the total recordable incidence rate : Using the provided Waldorf Widget Factory OSHA 300A log, calculate the total recordable incidence rate and the severity rate.
Write a function that generates a list of all prime numbers : Write a Python function that, given the path to a directory, lists the files in the directory (Hint: The function is actually quite simple, but it will require
Identify criteria for effective storytelling : Identify storytelling techniques and recommendations that can make your multimedia lessons more conducive to learning. Identify criteria for effective storytell
Describe where you experienced human resource dilemma : Describe scenario where you experienced human resource dilemma. Some examples- motivation, interpersonal relationships, or supervisor-subordinate interfaces.
How the interaction will better achieve your learning goals : Determining how the interaction will better achieve your learning goals. Explore are applicable to a variety of multimedia learning solutions.
Apply biomechanical methods to process the collected data : CAB201 Programming Principles Assignment: Project - Genomic Sequence Retrieval - Part I, Queensland University of Technology, Australia

Reviews

Write a Review

Python Programming Questions & Answers

  Write a python program to implement the diff command

Without using the system() function to call any bash commands, write a python program that will implement a simple version of the diff command.

  Write a program for checking a circle

Write a program for checking a circle program must either print "is a circle: YES" or "is a circle: NO", appropriately.

  Prepare a python program

Prepare a Python program which evaluates how many stuck numbers there are in a range of integers. The range will be input as two command-line arguments.

  Python atm program to enter account number

Write a simple Python ATM program. Ask user to enter their account number, and print their initail balance. (Just make one up). Ask them if they wish to make deposit or withdrawal.

  Python function to calculate two roots

Write a Python function main() to calculate two roots. You must input a,b and c from keyboard, and then print two roots. Suppose the discriminant D= b2-4ac is positive.

  Design program that asks user to enter amount in python

IN Python Design a program that asks the user to enter the amount that he or she has budget in a month. A loop should then prompt the user to enter his or her expenses for the month.

  Write python program which imports three dictionaries

Write a Python program called hours.py which imports three dictionaries, and uses the data in them to calculate how many hours each person has spent in the lab.

  Write python program to create factors of numbers

Write down a python program which takes two numbers and creates the factors of both numbers and displays the greatest common factor.

  Email spam filter

Analyze the emails and predict whether the mail is a spam or not a spam - Create a training file and copy the text of several mails and spams in to it And create a test set identical to the training set but with different examples.

  Improve the readability and structural design of the code

Improve the readability and structural design of the code by improving the function names, variables, and loops, as well as whitespace. Move functions close to related functions or blocks of code related to your organised code.

  Create a simple and responsive gui

Please use primarily PHP or Python to solve the exercise and create a simple and responsive GUI, using HTML, CSS and JavaScript.Do not use a database.

  The program is to print the time

The program is to print the time in seconds that the iterative version takes, the time in seconds that the recursive version takes, and the difference between the times.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd