代做Assignment 3代写Python语言

Assignment 3: due 11:59 pm on Saturday, Oct 19, 2024

To clarify Part 1 specifications, I have provided sample tests in Section 3.  The behaviour implied by the sample tests should be considered as required specifications in addition to what is explained below.

Summary of Instructions 

Note                                    Read the instructions carefully and follow them exactly

Assignment Weight           4% of your course grade

Due Date and time            11:59 pm on Saturday, Oct 19, 2024

Important

As outlined in the syllabus, late submissions will not be accepted

Any files with syntax errors will automatically be excluded from grading. Be sure to test your code before you submit it

For all functions, both in Part 1 and 2, make sure you’ve written good docstrings that include type contract, function description and the preconditions if any.

This is an individual assignment. Please review the Plagiarism and Academic Integrity policy presented in the first class, i.e.  read in detail pages  16-20 of course outline  (i.e.  slides of Lecture  1).  You can find that file on Brightspace under Lecture 1. While at it, also review Course Policy on missing assignments on page 14.

The goal of this assignment is to learn and practice the concepts covered thus far, in particular:  strings  (including indexing,  slicing and string methods),  control structures  (if statements and for-loops), use of range function, function design and function calls.

The only collection you can use are strings.  You may not use any other collection  (such as a list/array, tuple, set or dictionary) in this  assignment.  Using any of these in a solution to a question constitutes changing that  question. Consequently, that question will not be graded.

Your grade will partially be determined by automatic  (unit) tests that will test your  functions. All  the  specified requirements below are mandatory (including function names).  Any requirement that is specified and not met may/will result in deduction of points.

Submit your assignment by the deadline via Brightspace (as instructed and practiced in the first lab.) You can make multiple submissions,  but only the last submission before the deadline will be graded.   For this  assignment you must submit 5 files as described below.  For each missing file there will be a grade deduction:

The assignment has two parts.  Each part explains what needs to be submitted.  Put all those required documents into a folder called a3_xxxxxx where you changed xxxxxx to your student number, zip that folder (do not use rar compression format) and submit it as explained in Lab 1. In particular, the folder must have the following 5 files:

Part 1:  a3_part1_xxxxxx.py, a3_part1_xxxxxx.txt

Part 2:  a3_part2_xxxxxx.py and a3_part2_xxxxxx.txt and

references-YOUR-FULL-NAME .txt

Both of your programs must run without syntax errors.  In particular, when grading your assignment, TAs will first open your file a3_part1_xxxxxx.py with IDLE and press Run Module.  If pressing Run Module causes any syntax error, the grade for Part 1 becomes zero.  The same applies to Part 2, when they open and run file a3_part2_xxxxxx.py.

Furthermore, for each of the functions (in Part 1 and Part 2), I have provided one or more tests to test your functions with.   For  example, you should test the function split_tester from Part 1 by making a call in Python shell with split_tester  ("12311234","4") .  To obtain a partial mark your function may not necessarily give the correct answer on these tests.  But if your function gives any kind of python error when run on the tests provided below, that question will be marked with zero points.

Section 3, contains tests for Part 1.  Tests for Part 2 are provided after each question in Part 2.  To determine your grade, your functions will be tested both with examples provided in Part 2 and Section 3 and with some other examples. Thus you too should test your functions with more examples than what I provided in Part 2 and Section 3.

Use of global variables inside function bodies is not allowed.  If you do not know what that means, interpret this to mean that inside of your functions you can only use variables that are created in that function.  For example, this is not allowed, since variable x is not a parameter of function a_times(a) nor is it a variable created in function a_times(a). It is a global variable created outside of all functions.

def  a_times(a):

result=x*a

return  result

x=float(input("Give me  a  number:  "))

print(a_times(10))

About references-YOUR-FULL-NAME . txt le:

The file must be a plain text file.  The file must contain references to any code you used that you did not write yourself, including any code you got from a friend, internet, AI engines like chatGPT, social media/forums (including Stack Overflow and discord) or any other source or a person.   The only exclusion from that rule is the code that we did in class, the code done as part the lab work, or the code in your textbook.  So here is what needs to be written in that file.  For every question where you used code from somebody else:

  Write the question number

•  .  Copy-paste all parts of the code that were written by somebody else.  That includes the code you found/were-given and that you then slightly modified.

•  Source of the copied code: name of the person or the place on the internet/book where you found it.

While you may not get points for copied parts of the question, you will not be in the position of being accused of plagiarism. Any student caught in plagiarism will receive zero for the whole assignment and will be reported to the dean.

Showing/giving any part of your assignment code to a friend also constitutes plagiarism and the same penalties will apply.  If you have nothing to declare/reference, then just write a sentence stating that and put your first and last name under that sentence in your references-YOUR-FULL-NAME.txt file.

Not including references-YOUR-FULL-NAME.txt file, will be taken as you declaring that all the code in the assignment was written by you.  Recall though that not submitting that file, comes with a grade penalty.

1    Part 1:  increasing-splits tester - 40 points

To clarify Part  1 specifications, I have provided sample tests in Section 3.  The behaviour implied by the sample tests should be considered as required specifications in addition to what is explained below.

Description:  Some positive integers N can be split into smaller pieces, such that the pieces are comprised of consecutive digits of N and such that each piece contains the same number of digits.  Given an integer N and given a number of digits d, one can then ask if N can be split into pieces such that each pieces has d digits and such that the resulting sequence of numbers is strictly increasing.

Examples:

- For N = 154152 and d = 2, the answer is yes, since d = 2 implies the following sequence  15, 41, 52 which is increasing.

- For N = 154152 and d = 3, the answer is no, since d = 2 implies the following sequence 154, 152 which is not increasing.

- For N = 154152 and d = 5, the answer is no, since  154152 cannot be split into ve-digit pieces.

- For N = 137 and d = 1, the answer is yes, since since d = 1 implies the following sequence  1, 3, 7 which is increasing.

- For N = 137 and d = 2, the answer is no, since 137 cannot be split into two-digit pieces.

- For N = 113 and d = 1, the answer is no, since since d = 1 implies the following sequence 1, 1, 3 which is not increasing.

- For N = 113 and d = 3, the answer is yes, since d = 3 implies the following sequence 113 which is increasing.

Design, implement and test a Python program which checks to see if a user-supplied positive integer N and a user- specified split d, are such that N can be split into pieces with d digits such that the resulting sequence is strictly increasing. Here are the steps that your program should have:  (Note that since no collection other than strings is allowed you will be working with N and d as strings)

0.  The program greets the user.

1.  The program asks the user if they want to test if a number admits an increasing-split of give size.

2.  The program prompts the user to enter a positive integer.  If the user enters an invalid input (anything other than a positive integer), the program will repeat the questions starting with step 1.

3.  The program prompts the user to enter the number of digits in each piece.  The program will verify that the input is valid (a positive integer which is a proper divisor of the number of digits in the first input); If the input is invalid, the program will repeat the questions starting with step 1.

4.  Once the valid input is obtained, the program splits the number into pieces of specified length and displays/prints those pieces in one line (separated by commas).

5.  Finally, the program reports whether or not the obtained sequence of numbers is in strictly increasing order.  If there is only one piece, it is defined to be in strictly increasing order.

For this part, I provided you with starter code in file called part1_xxxxxx.py.  Begin by replacing xxxxxx in the file name with your student number.  Then open the file. Your solution (code) for this part must go into that file in the clearly indicated spaces only. You are not allowed to delete or comment-out or change any parts of the provided code except for the keyword pass.

For this part you need to submit two files:  a3_part1_xxxxxx.py and a3_part1_xxxxxx.txt

a3_part1_xxxxxx.py needs to contain your program for Part 1 as explained above and a3_part1_xxxxxx.txt needs to contain the proof that you tested your two core function from this part, namely split_tester.

1.1    The Core Function

Your solution in a3_part1_xxxxxx.py must have a function called:  split_tester.  You should design and test function split_tester before moving onto designing and coding the main part of the program.  Here are specifications for that function:

split_tester  This function has two parameters, a string N and a string d.  These two strings can be assume to be such that each looks like a positive integer and such that the number of digits in N is divisible by the integer represented by d.

Since N and d are assumed to be valid i.e.  meet the preconditions, they define a sequence of numbers (as specified in the examples above).  The function should print that sequence of numbers in such a way that the consecutive numbers are separated by commas as shown in the test cases.  Note that there cannot be a comma after the last number in the sequence.

The function should then return True if the sequences is strictly increasing and False otherwise.   (Reporting, i.e. printing, whether or not the obtained sequence of numbers is strictly increasing has to be done in the main and not in the body of the split_tester function).

Here are two approaches that may help you design this function:

You may find it easier to process the number N, which is given as a string, when you split it into pieces.

One approach:

start  with  an  empty  string  to  hold  the  substring,  i .e.  the  split

as  you  loop  through  the  whole  number  one  digit  at  a  time

append  digits  to  the  substring

if  the  substring  reaches  the  desired  length

do  something  with  the  substring

reset  the  substring  to  empty  to  collect  the  next  substring

reset  the  substring  length  count  to  zero 

Another approach:

figure  out  how many  substrings  you  want

loop  that  number  of  times

use  slices  to  create  the  substrings

Other suggestions:

-You can use the isdigit() string method to determine if a string contains only digits.  Type help(str.isdigit) in the Python shell for more information.

- You can use the len() function to determine the length of a string i.e.  the number of digits in N.

- If you find getting the commas right in the output tricky  (because only commas between numbers are allowed), leave that until everything else is working correctly.

1.2    The User Interaction i.e.  the main part of the program

Now that you have the function that performs the core functionality, you want to code the communication with the user in the main.  It is also in the main that you should print the message about whether the sequence is increasing or not.

Advice:  Leave error checking of the input for last.  Begin by building your function first, and then the main assuming perfect input.  Make sure though to leave enough time at the end to get the input checking details right.

The specifications on how your program needs to behave when communicating with the user should be inferred from the test examples.  You will notice that the program is required to display greetings surrounded with stars.  One of your functions from Assignment 1, may be helpful for that. You can copy paste that function from your Assignment 1 solution (or mine) to your solution in a3_part1_xxxxxx.py.

2    Part 2: A Library of Functions

For this part of the assignment, you are required to write and test several functions (as you did in Assignment 1).  You need to save all functions in a3_part2_xxxxxx.py where you replace xxxxxx by your student number.  You need to test your functions  (like you did in Assignment 1) and copy/paste your tests in  a3_part2_xxxxxx.txt.  Thus, for this part you need to submit two files:  a3_part2_xxxxxx.py and a3_part2_xxxxxx.txt

2.1    sum_odd_divisors(n) - 5 points

Write a function called sum_odd_divisors that takes a integer n as input.  If n is zero, sum_odd_divisors returns None. Else, sum_odd_divisors returns the sum of all the postive odd divisors of n.

>>>  sum_odd_divisors(-9)

13

>>>  sum_odd_divisors(1)

1

>>>  sum_odd_divisors(2)

1

>>>  sum_odd_divisors(3)

4

>>>  sum_odd_divisors(7)

8

>>>  sum_odd_divisors(-2001)

2880

2.2    series_sum() - 5 points

Write a function called series_sum() that prompts the user for an non-negative integer n.  If the user enters a negative integer the function should return None otherwise the function should return the sum of the following series 1000 + 1/12 + 1/22 + 1/32 + 1/42 + ... + 1/n2  for the given integer n. For example, for n = 0, the function should return 1000, for n = 1, the function should return  1001, for n = 2, the function should return  1001.25, for n = 3, the function should return 1001.3611111111111, etc.

>>>  series_sum()

Please  enter  a  non-negative  integer:  -10

>>>  series_sum()

Please  enter  a  non-negative  integer:  0

1000

>>>  series_sum()

Please  enter  a  non-negative  integer:  5

1001.463611111111

2.3    pell(n) - 5 points

Pell numbers are a mathematical sequence of numbers that help approximate the value of 2 (check out the Wikipedia page on Pell Numbers:

https://en.wikipedia.org/wiki/Pell__number).  The sequence is defined recursively as shown in eq.

                    (1)

Write a function called pell that takes one integer parameter n, of type int.  If n is negative, pell returns None.  Else, pell returns the nth Pell number (i.e.  Pn ).

>>>  pell(0)

0

>>>  pell(1)

1

>>>  pell(2)

2

>>>  pell(3)

5

>>>  pell(-45)

>>>  pell(6)

70

>>>  pell(1743)

532690007842574349527112009340648584206039420554652235605917968301878560204391358977288970768088948931512979242757675577993 290392759079609635614256071618406298994532643385226638853102443318071341757012888307627361739142916789755393486051687770688 826270892256294622920292002671354119783127549690735964749390914433090966928985482211622995711178437113046183531202406642424 338067044223749234565988260287660191555741946139008489540633861672714251246514572808657156171836659007522426709678946690131 627254854511941937501064402371005017444986539854908064857327597139176688814079527858978412577793058938555244277033473983250 7514635151419472104046900160626484896467099596437905

2.4    countMembers(s) - 5 points

In this question you are not allowed to use any of the Python,s string methods (i.e.  those string functions that you call with dot operator).

Say a character is extraordinary if it is one of the following: the lower case letter between e and j (inclusive), the upper case letters between F and X (inclusive), numerals between 2 and 6 (inclusive), and the exclamation point ( !), comma ( ,), and backslash (\)

You are required to count how many times these characters appear in a string.

Write a function called countMembers that takes a single input parameter s, of type str.  countMembers then returns the number of characters in s, that are extraordinary.  Therefore, if there are two Xs in s, countMembers must count two extraordinary characters (one for each occurrence of X in s).

>>>  countMembers("\\")

1

>>>  countMembers("2\ ' ")

1

>>>  countMembers("1\ ' ")

0

>>>  countMembers("2aAb3?eE' _13")

4

>>>  countMembers("one,  Two") 3

2.5    casual_number(s) - 5 points

Imagine a customer in a bank is asked to enter a whole number representing their approximate monetary worth.  In that case, some people use commas to separate thousands, millions etc, and some don,t.  In order to perform a some calculations the bank needs that number as na integer.  Write a function, called  casual_number(s) that has one parameter,  s, as input where s is a string.  Function casual_number(s) should return an integer representing a number in s.  If s does not look like a number the function should return None. Note that if a string in s looks like a number but with commas, you may assume that commas are in meaningful places i.e. you may assume that s will not be a string like ,1; 1; 345,.

>>>  casual_number("251")

251

>>>  casual_number("1,aba,251")

>>>  casual_number("1,251")

1251

>>>casual_number("1251")

1251

>>>  casual_number("-97,251")

-97251

>>>  casual_number("-97251")

-97251

>>>  casual_number("-,,,,")

>>>  casual_number("--97,251")

>>>  casual_number("-")

>>>  casual_number("-1,000,001")

-1000001

>>>  casual_number("999,999,999,876") 999999999876

>>>  casual_number("-2")

-2

2.6    alienNumbers(s) - 5 points

Strange communication has been intercepted between two alien species at war.   NASAs top linguists have determined that these aliens use a weird numbering system.  They have symbols for various numeric values, and they just add all the values for the symbols in a given numeral to calculate the number.  NASA’s linguists have given you the following table of symbols and their numeric values.  Since they have a lot of these numbers to compute, they want you to write a function that they can use to automate this task.

Thus a!ya!U!NaU represents a value of 1095 (see table below for an explanation).


Write a function called alienNumbers that takes one string parameter s, and returns the integer value represented by

s.  Since aliens only know these characters you may assume that no character in s outside of this set:  {T,y,  !,a,  N,  U}.

Callenge: try to make the whole body of this function only one line long.

>>>  alienNumbers("a!ya!U!NaU")

1095

>>>  alienNumbers("aaaUUU")

129

>>>  alienNumbers("") 0

2.7    alienNumbersAgain(s) - 5 points

NASA is very pleased with your proof-of-concept solution in the previous question.  Now, they’ve designed a small chip that runs a very restricted version of python - it doesn’t have any of the string methods that you are used to.   They want you to now rewrite your alienNumbers function to run without using any of those string methods you may have otherwise used.  Basically, you can use a loop of some sort and any branching you’d like, but none of the string methods. Use accumulator variable as we have seen in class. ’

Write a function called alienNumbersAgain, that takes a single string parameter s, and returns the numeric value of the number that s represents in the alien numbering system.

>>>  alienNumbersAgain("a!ya!U!NaU")

1095

>>>  alienNumbersAgain("aaaUUU")

129

>>>  alienNumbersAgain("")

0

The last three questions may be a more challenging to solve especially the last one.



热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图