Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 8.03 KB | None | 0 0
  1. object Day02 extends App{
  2.  
  3.   val map1 = Map(0 -> 1, 1 -> 2, 2 -> 0, 3 -> 0, 4 -> 3)
  4.   val map2 = Map(0 -> 1, 1 -> 2)
  5.  
  6.  
  7.  
  8.  
  9.   def isPalindrome(word: String): Boolean = {
  10.     var refinedWord = word.toLowerCase
  11.     refinedWord = refinedWord.replaceAll("[.,!?\"\' ]", "")
  12.     val reversedWord = refinedWord.reverse
  13.     if (refinedWord.equals(reversedWord)) {
  14.       true
  15.     }
  16.     else {
  17.       false
  18.     }
  19.   }
  20.  
  21.   println("Palindrome Function")
  22.   println("======================================================")
  23.   println(isPalindrome("Madam, I'm Adam."))
  24.   println(isPalindrome("Sit, on a potato pan Otis."))
  25.   println()
  26.   println()
  27.   println()
  28.  
  29.  
  30.   def fib(limit: Int): Int = {
  31.     if (limit<=2) 1
  32.     else fib(limit-1) + fib(limit-2)
  33.   }
  34.  
  35.   println("Fib Function")
  36.   println("======================================================")
  37.   println(fib(10))
  38.   println()
  39.   println()
  40.   println()
  41.  
  42.  
  43.   def reverseSentence(words: String):String = {
  44.     val wordToList = words.split(" ")
  45.     val reversedList = wordToList.reverse
  46.     val newString = reversedList.mkString(" ")
  47.     newString
  48.   }
  49.  
  50.   println("ReverseSentence Function")
  51.   println("======================================================")
  52.   println(reverseSentence("Hello World"))
  53.   println(reverseSentence("Cooper is my name"))
  54.   println()
  55.   println()
  56.   println()
  57.  
  58.  
  59.   def addMembersToClub(): Unit = {
  60.     var condition = true
  61.     val list = collection.mutable.ListBuffer[String]()
  62.     while (condition) {
  63.       println("Please enter a name you want to add to the club")
  64.       val name = scala.io.StdIn.readLine()
  65.       if (name == "done") {
  66.         condition = false
  67.         println(list)
  68.       } else {
  69.         list += name
  70.       }
  71.     }
  72.   }
  73.  
  74.   println("addMembersToClub Function")
  75.   println("======================================================")
  76.   println(addMembersToClub())
  77.   println()
  78.   println()
  79.   println()
  80.  
  81.  
  82.  
  83.   def passwordValid(): Boolean = {
  84.     println("Write your password: ")
  85.     val input = scala.io.StdIn.readLine()
  86.  
  87.     val alphabetLower = ('a' to 'z').toList
  88.     val alphabetUpper = ('A' to 'Z').toList
  89.     val numbers = ('0' to '9').toList
  90.     val symbols = List('$', '#', '@')
  91.     val mergedList = alphabetLower:::alphabetUpper:::numbers:::symbols
  92.  
  93.     var lowerCount = 0
  94.     var upperCount = 0
  95.     var numberCount = 0
  96.     var characterCount = 0
  97.     val minLength = 6
  98.     val maxLength = 16
  99.  
  100.     var condition = true
  101.  
  102.     if (minLength <= input.length() && input.length() <= maxLength) {
  103.       for (i <- input.toCharArray) {
  104.         if (!mergedList.contains(i.toChar)) {
  105.           condition = false
  106.         } else {
  107.           if (alphabetLower.contains(i)) {
  108.             lowerCount += 1
  109.           }
  110.           if (alphabetUpper.contains(i)) {
  111.             upperCount += 1
  112.           }
  113.           if (numbers.contains(i)) {
  114.             numberCount += 1
  115.           }
  116.           if (symbols.contains(i)) {
  117.             characterCount += 1
  118.           }
  119.         }
  120.       }
  121.     }
  122.     if (!(lowerCount > 0 && upperCount > 0 && numberCount > 0 && characterCount > 0 )) {
  123.       condition = false
  124.     }
  125.     condition
  126.   }
  127.  
  128.   println("passwordValid Function")
  129.   println("======================================================")
  130.   println(passwordValid())
  131.   println()
  132.   println()
  133.   println()
  134.  
  135.  
  136.   def numberToAverage(number: Int): Unit = {
  137.     if (number > 1) {
  138.       println(s"Average is ${number/2} and the number is $number")
  139.     } else {
  140.       println("Please use a number greater than 1 next time")
  141.     }
  142.   }
  143.  
  144.   println("numberToAverage Function")
  145.   println("======================================================")
  146.   println(numberToAverage(100))
  147.   println()
  148.   println()
  149.   println()
  150.  
  151.  
  152.   def printEven(limit: Int): Unit = {
  153.     for (i <- 0 to limit)
  154.       if (i % 2 == 0) {
  155.         println(i)
  156.       }
  157.   }
  158.  
  159.   println("printEven Function")
  160.   println("======================================================")
  161.   println(printEven(100))
  162.   println()
  163.   println()
  164.   println()
  165.  
  166.   def nextDay(): Unit = {
  167.     val yearList = Map(1 -> 31, 2 -> 28, 3 -> 31, 4 -> 30, 5 -> 31, 6 -> 30, 7 -> 31, 8 -> 31, 9 -> 30, 10 -> 31, 11 -> 30, 12 -> 31)
  168.  
  169.     println("Enter a year:")
  170.     val year = scala.io.StdIn.readLine()
  171.  
  172.     println("Enter a month:")
  173.     val month = scala.io.StdIn.readLine()
  174.  
  175.     println("Enter a day:")
  176.     val day = scala.io.StdIn.readLine()
  177.  
  178.     try {
  179.       var yearInt = year.toInt
  180.       var monthInt = month.toInt
  181.       var dayInt = day.toInt
  182.  
  183.       if (yearList.contains(monthInt)) {
  184.         // if we are on the last day of the month cycle to next month and begin day 1
  185.         if (dayInt == yearList(monthInt)) {
  186.           // if we are on the last day of december increase the year and go to Jan 1st
  187.           if (monthInt == 12) {
  188.             yearInt += 1
  189.             monthInt = 1
  190.             dayInt = 1
  191.           }
  192.           else {
  193.             monthInt += 1
  194.             dayInt = 1
  195.           }
  196.         } else {
  197.           dayInt += 1
  198.         }
  199.         println(s"Year: $yearInt, Month: $monthInt, Day: $dayInt")
  200.       } else {
  201.         println("Your month number is invalid. Please chose a number from 1-12")
  202.       }
  203.  
  204.     } catch {
  205.       case e: NumberFormatException => {
  206.         println("Please enter only numbers next time")
  207.       }
  208.     }
  209.  
  210.   }
  211.  
  212.   println("nextDay Function")
  213.   println("======================================================")
  214.   println(nextDay())
  215.   println()
  216.   println()
  217.   println()
  218.  
  219.  
  220.   def uniqueValueFinder(dict: Map[Int, Int]): Int = {
  221.     dict.values.toSet.toList.length
  222.   }
  223.  
  224.   println("uniqueValue Function")
  225.   println("======================================================")
  226.   val yearList = Map(1 -> 31, 2 -> 28, 3 -> 31, 4 -> 30, 5 -> 31, 6 -> 30, 7 -> 31, 8 -> 31, 9 -> 30, 10 -> 31, 11 -> 30, 12 -> 31)
  227.   println(uniqueValueFinder(yearList))
  228.   println()
  229.   println()
  230.   println()
  231.  
  232.  
  233.  
  234.   def dotProduct(map1: Map[Int, Int], map2: Map[Int, Int]): collection.mutable.Map[Int, Int] = {
  235.     val map = collection.mutable.Map[Int, Int]()
  236.     var product = 0
  237.     val largerMap = getLargerMap(map1, map2)
  238.  
  239.     for(i <- 0 until largerMap.values.toList.length) {
  240.       // same size maps
  241.       if (map1.contains(i) && map2.contains(i)) {
  242.         product = map1(i) * map2(i)
  243.         map += (i -> product)
  244.       }
  245.       if (map1.contains(i) && !map2.contains(i)) {
  246.         product = map1(i)
  247.         map += (i -> product)
  248.       }
  249.       if (!map1.contains(i) && map2.contains(i)) {
  250.         product = map2(i)
  251.         map += (i -> product)
  252.       }
  253.     }
  254.     removeMapZeros(map)
  255.   }
  256.  
  257.   println("dotProduct Function")
  258.   println("======================================================")
  259.   println(dotProduct(map1, map2))
  260.   println()
  261.   println()
  262.   println()
  263.  
  264.   def addVectors(map1: Map[Int, Int], map2: Map[Int, Int]): collection.mutable.Map[Int, Int] = {
  265.     val map = collection.mutable.Map[Int, Int]()
  266.     var sum = 0
  267.     val largerMap = getLargerMap(map1, map2)
  268.  
  269.     for(i <- 0 until largerMap.values.toList.length) {
  270.       // same size maps
  271.       if (map1.contains(i) && map2.contains(i)) {
  272.         sum = map1(i) + map2(i)
  273.         map += (i -> sum)
  274.       }
  275.       if (map1.contains(i) && !map2.contains(i)) {
  276.         sum = map1(i)
  277.         map += (i -> sum)
  278.       }
  279.       if (!map1.contains(i) && map2.contains(i)) {
  280.         sum = map2(i)
  281.         map += (i -> sum)
  282.       }
  283.     }
  284.     removeMapZeros(map)
  285.   }
  286.  
  287.   println("vectorSum Function")
  288.   println("======================================================")
  289.   println(addVectors(map1, map2))
  290.   println()
  291.   println()
  292.   println()
  293.  
  294.  
  295. //  #10 Helper Method
  296.  
  297.   def getLargerMap(map1: Map[Int, Int], map2: Map[Int, Int]): Map[Int, Int] = {
  298.     if (map1.values.toList.length > map2.values.toList.length) {
  299.       map1
  300.     } else {
  301.       map2
  302.     }
  303.   }
  304.  
  305.  
  306.   //  #10 Helper Method
  307.  
  308.   def removeMapZeros(map: collection.mutable.Map[Int, Int]) : collection.mutable.Map[Int, Int] = {
  309.     for (i <- 0 until map.toList.length) {
  310.       if (map(i) == 0) {
  311.         map.remove(i)
  312.       }
  313.     }
  314.     map
  315.   }
  316.  
  317.  
  318.  
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement