Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ID: aordano1
  3. LANG: TYPESCRIPT
  4. TASK: ride
  5. PROG: ride
  6. */
  7.  
  8.  
  9. /*
  10.  
  11. Problem statement:
  12.  
  13.  It is a well-known fact that behind every good comet is a UFO.
  14.  These UFOs often come to collect loyal supporters from here on Earth.
  15.  Unfortunately, they only have room to pick up one group of followers on each trip.
  16.  They do, however, let the groups know ahead of time which will be picked up for each comet by a clever scheme: they pick a name for the comet which, along with the name of the group, can be used to determine if it is a particular group's turn to go (who do you think names the comets?).
  17.  The details of the matching scheme are given below; your job is to write a program which takes the names of a group and a comet and then determines whether the group should go with the UFO behind that comet.
  18.  
  19. Both the name of the group and the name of the comet are converted into a number in the following manner: the final number is just the product of all the letters in the name, where "A" is 1 and "Z" is 26.
  20. For instance, the group "USACO" would be 21 * 19 * 1 * 3 * 15 = 17955. If the group's number mod 47 is the same as the comet's number mod 47, then you need to tell the group to get ready! (Remember that "a mod b" is the remainder left over after dividing a by b; 34 mod 10 is 4.)
  21.  
  22. Write a program which reads in the name of the comet and the name of the group and figures out whether according to the above scheme the names are a match, printing "GO" if they match and "STAY" if not.
  23. The names of the groups and the comets will be a string of capital letters with no spaces or punctuation, up to 6 characters long.
  24.  
  25. */
  26.  
  27. const generateName = () => {
  28.     const generateLetterCode = () => { return Math.round(Math.random() * 26) + 65 } // 65 is the offset so it corresponds to an ASCII char (A being 065)
  29.  
  30.     const name: string[] = []
  31.  
  32.     const nameLength: number = Math.ceil(Math.random () * 6)
  33.  
  34.     for (let i = 0; i < nameLength; i += 1) {
  35.         name.push(String.fromCharCode(generateLetterCode()))
  36.     }
  37.  
  38.     return String(name)
  39. }
  40.  
  41. const groupName = generateName()
  42. const cometName = generateName()
  43.  
  44. // Actually starts here, above only provided test data
  45.  
  46. const generateValue = (name: string) => {
  47.     const nameArray = name.split("")
  48.     const charValues = nameArray.map((value) => {
  49.         return value.charCodeAt(0) - 65
  50.     })
  51.    
  52.     let nameNumber: number = 1
  53.    
  54.     charValues.forEach((value) => {
  55.         nameNumber = value * nameNumber
  56.     })
  57.  
  58.     return nameNumber
  59. }
  60.  
  61. const compareNames = (groupName: string, cometName: string) => {
  62.     const groupValue = generateValue(groupName)
  63.     const cometValue = generateValue(cometName)
  64.  
  65.     const groupModValue = groupValue % 47
  66.     const cometModValue = cometValue % 47
  67.  
  68.     if (groupModValue === cometModValue) {
  69.         return "GO"
  70.     }
  71.  
  72.     return "STAY"
  73. }
  74.  
  75. compareNames(groupName, cometName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement