ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 9 Weeks Ago, 4 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 09 May 2017 My Price 9.00

6Programming Assignment — Due 17th Class

http://cs.nyu.edu/courses/fall16/CSCI-UA.0002-007/hw6.html

Home Work Assignment Number 6Programming Assignment — Due 17th Class

  1. Secret Code: write a function that takes a string as an argument and prints out 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 loop to solve the problem. The output could begin as an empty string and be built up to the final solution. For example 'cat' should be printed as: 99 97 116
  2. 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 went over the algorithm in class and it is repeated here. Please note that your program does not have to handle commas or other words like "and".
    1. Download the file: number_program_input.py
      1. This contains both some simple functions and variables to use for both the regular assignment and the extra credit assignment
    2. Examples that should be handled (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
    3. Algorithm
      1. First divide the string of words into a list of words using the "split" method
        1. e.g., 'Five hundred million two hundred three thousand seventeen'.split(' ')
      2. Convert each word into a corresponding integer, e.g, [5, 100, 1000000, 2, 100, 3, 1000 17]
        1. In order to do this you must have some mechanism for converting words that represent numbers into the numbers they represent (one, two, three, ... twenty, thirty, forty, ..., million, billion, etc.). For the examples, that you will need to convert, you will not need anything higher than billion.  You can use the function word_to_number from number_program_input.py
        2. This can be achieved by building a new list starting with [] and accumulating with the .append method
        3. Or it can be achieved by changing the list you derived using split. Going through the list one index number at a time and changing the list at that index position.
      3. Combine the numbers in the list from the previous step together in the following 3 passes (or more passes if you find it necessary to get the right answers). At each pass produce a new output list. For example the output of the first step could be called output1. output1 would be input to the 2nd step and the output could be called output2, etc. In the end, you will add up the numbers in the last output list and return the answer.
        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 those items that are larger than 999 and combined them together such that
            1. if lower_number precedes higher_number, replaced the combination by 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, only the sequence 5, 100 meets the condition that the first item is less than the second.
          1. This step requires that you use a variable to temporarily store each number (that is less than 1000), e.g., using a variable like hold, to store temporary values. This way you can check to see if a current number is greater or less than the previous value from the loop. If the previous value in hold is less than the current value, you multiply the two and save that in the final output, otherwise, you simply store the stored value and temporarily put the new value in hold.
        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
          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
          1. Example: [500, 1000000, 200, 3, 1000, 17] should be converted to [500, 1000000, 203, 1000, 17]
          2. This step is similar to the previous one, except: (a) this time you are looking to add numbers together rather than multiplying; (b) you are looking for pairs of numbers such that the first is greater than the second, rather than less than.
        1. Now you may have a sequence that has instances of 1000, 1000000, etc. plus integers between 1 and 999. Go through the new list twice and perform the following operations.
          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
      1. Return the answer (verify with the above examples that your program works correctly) and debug as necessary
      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.
        1. Note that the 2 bold-face steps 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.
  1. Extra Credit.  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
  2. number_program_input.py  (sample_chinese_number_string1, sample_chinese_number_string2, sample_chinese_number_string3).
    1. To tokenize, 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 2 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,000 (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 steps 2.3.3.1 and 2.3.3.2 separate)
      3. An Example follows:
        1. 九千二百万二千三百二十一
        2. [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
        3. [9000, 200, 10000, 2000, 300, 20, 1] ## add low numbers less than 10,000
        4. [9200, 10000, 2321] # multiply numbers 10,000 or greater with preceding low numbers
        5. [92000000, 2321] ## add remaining numbers
        6. 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)?

Answers

(11)
Status NEW Posted 09 May 2017 06:05 AM My Price 9.00

-----------

Attachments

file 1494312184-Solutions file 2.docx preview (51 words )
H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly -----------onl-----------ine----------- an-----------d g-----------ive----------- yo-----------u e-----------xac-----------t f-----------ile----------- an-----------d t-----------he -----------sam-----------e f-----------ile----------- is----------- al-----------so -----------sen-----------t t-----------o y-----------our----------- em-----------ail----------- th-----------at -----------is -----------reg-----------ist-----------ere-----------d o-----------n -----------THI-----------S W-----------EBS-----------ITE-----------. ----------- Th-----------ank----------- yo-----------u -----------
Not Rated(0)