代写Oop | Algorithm代做 | Assignment | java代做 – Assignment Number 6

代写Oop | Algorithm代做 | Assignment | java代做 – 这是一个基于java的算法代写任务

Home Work assignment Number 6

1. Secret Code
Write a function that takes a string as an argument parameter and returns a new string consisting
of numbers divided by spaces. These numbers should be derived by using the ord function on
each character. It is suggested that you use a for l oop to solve the problem. The output could be
an accumulator variable that you initialize as the empty string and then build up using the for
loop. For each character of the input string, you should add on the next character number derived
using ord. For example 'cat' should be printed as: 99 97 1 16
2. Adding -ing to verbs
Write a program similar to the morphology program for plurals discussed in class. Your program
should take a single word as input, modifying it as appropriate, and adding ing to the end of the
word. Specifically, given words in the Input column of the following table, it should return the
corresponding Output word. The decisions about how to modify the words should be based on
the final three letters of the input word. The status of these final 3 letters as vowels (a,e,i,o,u) or
consonants (other letters) is important. In some cases, the final letter of the input word should be
repeated before adding ing. In some cases, the final letter of the input word should be deleted
before adding ing. Remember to use slices to represent parts of words. See slides for details
about slices (using both positive and negative integers). Hint: to keep things simple, convert all
input to lower case, using word = word.lower() , before running your code on that word.
Input Output
sit sitting
seat seating
write writing
sleep sleeping
fly flying
flee fleeing
stomp stomping
be being
3. Converting Number Words into Integers
Write a program that convert sequences of numbers written out as words (e.g., 'five million three
hundred forty five thousand seven hundred three') into numbers (e.g., 5345703). We have gone
over the  Algorithm in class or will do so shortly. However, it is also repeated here. Please note
that your program does not have to handle commas or other words like "and" (although, you can
deal with these by simply deleting them if you want). Your goal is to handle the following
cases (see the global variable sample_arabic_number_strings in the number_program_input.py
file):
1. Five hundred million two hundred three thousand seventeen
2. One billion seventy three
3. One hundred ninety two thousand seven hundred thirty one
We will divide this program up into the following parts. It is suggested that you make a new
output list for each part and derive that output list from the output of the previous stage, i.e.,
input --> output1 --> output2 --> output3 --> ... --> final_output. There are other ways to do this,

but this method is easier to debug because it is easy to look at the output of each stage. Note: it is not necessary for your program to work for numbers of one trillion or greater.

1. Make a list of numbers
1. Download the file: number_program_input.py. This contains both some simple
functions and variables to use for both the regular assignment and the extra credit
assignment.
2. Split the input by spaces (using the "split" method), creating a list of words, e.g.,
'Five hundred million two hundred three thousand seventeen'.split(' ') -> ['Five',
'hundred', 'million', 'two', 'hundred', 'three', 'thousand', 'seventeen']
3. Use the word_to_number function in number_program_input.py to create a list of
integers from the list of words.
1. Initialize a new list as the empty list []
2. For each word in the list of words, derive one integer and append it to the new
list
3. Remember to be careful about the difference between upper and lower case
because word_to_number will return None , instead of an integer if its input is
upper case.
4. Example: ['Five', 'hundred', 'million', 'two', 'hundred', 'three', 'thousand',
'seventeen'] -> [5, 100, 1000000, 2, 100, 3, 1000 17]
2. Combine numbers less than 1000 : Combine the numbers under 1000 from the list from
the previous step together in the following 2 passes. The output from the first pass will be
the input to the second pass. The output (output2) from the second pass will be the input to
later stages. In the end, you will add up the numbers in the last output list and return the
answer. See examples from the slides 28 to 30 in the class lectures slides about sequences
(part 1). One of the walk-throughs combines the steps 1 and 2 below. The advantage of
keeping them separate is that it is easier to adapt your system for the extra credit problem.
1. Go through the list created in the previous step in a loop. Build a new output
list such that:
1. items that are greater than 999 (thousand, million, etc.) are unchanged from
the input list
2. go through numbers between items that are larger than 999 and combined
them together such that
1. if lower_number precedes higher_number, replace the combination by
lower_number * higher_number, e.g., [5, 100] is replaced by 500
2. if higher_number precedes lower_number, put both in the output
unchanged.
3. Example:
[5, 100, 1000000, 2, 100, 3, 1000 17] should be converted to:
[500, 1000000, 200, 3, 1000, 17]
In the example, the subsequences [5, 100] and [2, 100] both meet
the condition that the first item is less than the second.
4. One way to implement this is to use a variable to temporarily store
numbers that are less than 1000. In this description, I will call this
variable hold:
1. At each iteration in a for loop, your program does some checks
about current_number (the next number in the loop) and the
value of hold :
if current_number is greater than 999, then:
if hold > 0, append hold to output
Append the current_number to output (right after
hold)
Set hold to 0
else if hold equals zero
set hold to current_number
else if current_number is greater than hold
set hold to the product of hold and current_number
else
append the value of hold to output
and append current_number to output
and set hold to 0
2. When the loop is complete, if hold is non-zero, append it to
output
3. For example, your program may need to "see" what comes after a
5, before appending it to the next output list. If 100 follows the 5,
500 should be appended to output , replacing both 5 and 100 in
the original sequence. The proposed strategy is to hold on to the 5
until checking what the next number is.
  1. Go through the output of the previous step to produce a new output list:
1. items that are greater than 999 (thousand, million, etc.) are unchanged (as in
the previous step)
2. go through numbers between those items that are larger than 999 and add
them together, storing the sum
For example, [200, 3] is replaced by 203 in the next output list (use hold
similarly as in the previous step)
3. Example: [500, 1000000, 200, 3, 1000, 17] should be converted to [500,
1000000, 203, 1000, 17]
4. This step is similar to the previous one, except: (a) this time you are adding
numbers together rather than multiplying them; (b) you are combining pairs of
numbers such that the first is greater than the second, rather than less than.
3. Combine numbers greater than 999 with numbers 999 or less. Go through the new list
twice and perform the following operations. This list consists of instances of 1000,
1000000, etc. plus integers between 1 and 999.
1. On the first pass, multiply the terms from 1-999 with following terms greater
than 999, e.g., [430, 1000000, 567, 1000, 35] --> [430000000, 567000, 35]
2. On the final pass, add the remaining terms together to get a new sum
[430000000, 567000, 35] --> 430567035. Return the answer (verify with the
above examples that your program works correctly) and debug as necessary.
4. Other notes
1. Note that the 1st 2 steps of part 2 can be combined into one step. However, then it
would be more difficult to generalize the algorithm to the Chinese case in the extra
credit.
2. Note that there are other ways of doing this task, but all of them involve looping
through the numbers several times and comparing two consecutive positions. The
program described above uses the variable hold to look back at previous items while
cycling through a loop. hold keeps track of intermediate calculations, while output
records parts of the problem which have been completed. There is an alternative
strategy in which you look forward -- for each item in the list, you look forward at
the next item before deciding what to do with it. If you choose to do this strategy
instead, you will have to be careful about not looking off the end of the list (past the
last item) because it will lead to the system crashing ("index out of range" errors).
Other aspects of this method will also be different.
  1. (Extra Credit) Chinese Numbers to Arabic Integers
1. Now lets apply almost the same algorithm to the problem of converting Chinese numbers to
Arabic numbers (the ones used in most European languages). Make a program that can translate
the example Chinese numbers listed in the variable sample_chinese_number_strings from
number_program_input.py
1. To split a string of chinese number characters into a list, convert the string to a list using
the function list, i.e., list('string') --> ['s','t','r','i','n','g']
2. Convert each character to an integer using the function
chinese_character_to_arabic_number from number_program_input.py
3. Use almost the same program as for question 3 to convert this list into an (arabic) number,
i.e., an integer
1. The main difference is that on the first pass, combine all numbers less than 10,
(instead of those less than 1000). In other words, rather than grouping numbers in 3s
(333,100,567,888), numbers are grouped in 4s (3331,0056,7888).
2. In addition, it is important to do all multiplication on one pass and then all addition
in a second pass. (i.e, keep Part 2 steps 1 and 2 separate)
3. An Example follows:
2. 
3. [9, 1000, 2, 100, 10000, 2, 1000, 3, 100, 2, 10, 1] ## multiply low numbers
less than 10,000, e.g., 2 must be combined with 100, before 200 can be added
to 9000
4. [9000, 200, 10000, 2000, 300, 20, 1] ## add low numbers less than 10,
5. [9200, 10000, 2321] # multiply numbers 10,000 or greater with preceding low
numbers
6. [92000000, 2321] ## add remaining numbers
7. 92002321
4. To make sure your program gets the right answers, see the comments under each of the
variable declarations for the Chinese numbers.

Criteria for grading these programs:

  1. Is it clear what you are doing: appropriate variable and function names, good comments,^ etc.
  2. Does the code get the correct answers?^
  3. Do you approximately implement the algorithms described in the questions^ (or improve upon them)?

发表评论

电子邮件地址不会被公开。 必填项已用*标注