Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Day02 extends App{
- val map1 = Map(0 -> 1, 1 -> 2, 2 -> 0, 3 -> 0, 4 -> 3)
- val map2 = Map(0 -> 1, 1 -> 2)
- def isPalindrome(word: String): Boolean = {
- var refinedWord = word.toLowerCase
- refinedWord = refinedWord.replaceAll("[.,!?\"\' ]", "")
- val reversedWord = refinedWord.reverse
- if (refinedWord.equals(reversedWord)) {
- true
- }
- else {
- false
- }
- }
- println("Palindrome Function")
- println("======================================================")
- println(isPalindrome("Madam, I'm Adam."))
- println(isPalindrome("Sit, on a potato pan Otis."))
- println()
- println()
- println()
- def fib(limit: Int): Int = {
- if (limit<=2) 1
- else fib(limit-1) + fib(limit-2)
- }
- println("Fib Function")
- println("======================================================")
- println(fib(10))
- println()
- println()
- println()
- def reverseSentence(words: String):String = {
- val wordToList = words.split(" ")
- val reversedList = wordToList.reverse
- val newString = reversedList.mkString(" ")
- newString
- }
- println("ReverseSentence Function")
- println("======================================================")
- println(reverseSentence("Hello World"))
- println(reverseSentence("Cooper is my name"))
- println()
- println()
- println()
- def addMembersToClub(): Unit = {
- var condition = true
- val list = collection.mutable.ListBuffer[String]()
- while (condition) {
- println("Please enter a name you want to add to the club")
- val name = scala.io.StdIn.readLine()
- if (name == "done") {
- condition = false
- println(list)
- } else {
- list += name
- }
- }
- }
- println("addMembersToClub Function")
- println("======================================================")
- println(addMembersToClub())
- println()
- println()
- println()
- def passwordValid(): Boolean = {
- println("Write your password: ")
- val input = scala.io.StdIn.readLine()
- val alphabetLower = ('a' to 'z').toList
- val alphabetUpper = ('A' to 'Z').toList
- val numbers = ('0' to '9').toList
- val symbols = List('$', '#', '@')
- val mergedList = alphabetLower:::alphabetUpper:::numbers:::symbols
- var lowerCount = 0
- var upperCount = 0
- var numberCount = 0
- var characterCount = 0
- val minLength = 6
- val maxLength = 16
- var condition = true
- if (minLength <= input.length() && input.length() <= maxLength) {
- for (i <- input.toCharArray) {
- if (!mergedList.contains(i.toChar)) {
- condition = false
- } else {
- if (alphabetLower.contains(i)) {
- lowerCount += 1
- }
- if (alphabetUpper.contains(i)) {
- upperCount += 1
- }
- if (numbers.contains(i)) {
- numberCount += 1
- }
- if (symbols.contains(i)) {
- characterCount += 1
- }
- }
- }
- }
- if (!(lowerCount > 0 && upperCount > 0 && numberCount > 0 && characterCount > 0 )) {
- condition = false
- }
- condition
- }
- println("passwordValid Function")
- println("======================================================")
- println(passwordValid())
- println()
- println()
- println()
- def numberToAverage(number: Int): Unit = {
- if (number > 1) {
- println(s"Average is ${number/2} and the number is $number")
- } else {
- println("Please use a number greater than 1 next time")
- }
- }
- println("numberToAverage Function")
- println("======================================================")
- println(numberToAverage(100))
- println()
- println()
- println()
- def printEven(limit: Int): Unit = {
- for (i <- 0 to limit)
- if (i % 2 == 0) {
- println(i)
- }
- }
- println("printEven Function")
- println("======================================================")
- println(printEven(100))
- println()
- println()
- println()
- def nextDay(): Unit = {
- 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)
- println("Enter a year:")
- val year = scala.io.StdIn.readLine()
- println("Enter a month:")
- val month = scala.io.StdIn.readLine()
- println("Enter a day:")
- val day = scala.io.StdIn.readLine()
- try {
- var yearInt = year.toInt
- var monthInt = month.toInt
- var dayInt = day.toInt
- if (yearList.contains(monthInt)) {
- // if we are on the last day of the month cycle to next month and begin day 1
- if (dayInt == yearList(monthInt)) {
- // if we are on the last day of december increase the year and go to Jan 1st
- if (monthInt == 12) {
- yearInt += 1
- monthInt = 1
- dayInt = 1
- }
- else {
- monthInt += 1
- dayInt = 1
- }
- } else {
- dayInt += 1
- }
- println(s"Year: $yearInt, Month: $monthInt, Day: $dayInt")
- } else {
- println("Your month number is invalid. Please chose a number from 1-12")
- }
- } catch {
- case e: NumberFormatException => {
- println("Please enter only numbers next time")
- }
- }
- }
- println("nextDay Function")
- println("======================================================")
- println(nextDay())
- println()
- println()
- println()
- def uniqueValueFinder(dict: Map[Int, Int]): Int = {
- dict.values.toSet.toList.length
- }
- println("uniqueValue Function")
- println("======================================================")
- 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)
- println(uniqueValueFinder(yearList))
- println()
- println()
- println()
- def dotProduct(map1: Map[Int, Int], map2: Map[Int, Int]): collection.mutable.Map[Int, Int] = {
- val map = collection.mutable.Map[Int, Int]()
- var product = 0
- val largerMap = getLargerMap(map1, map2)
- for(i <- 0 until largerMap.values.toList.length) {
- // same size maps
- if (map1.contains(i) && map2.contains(i)) {
- product = map1(i) * map2(i)
- map += (i -> product)
- }
- if (map1.contains(i) && !map2.contains(i)) {
- product = map1(i)
- map += (i -> product)
- }
- if (!map1.contains(i) && map2.contains(i)) {
- product = map2(i)
- map += (i -> product)
- }
- }
- removeMapZeros(map)
- }
- println("dotProduct Function")
- println("======================================================")
- println(dotProduct(map1, map2))
- println()
- println()
- println()
- def addVectors(map1: Map[Int, Int], map2: Map[Int, Int]): collection.mutable.Map[Int, Int] = {
- val map = collection.mutable.Map[Int, Int]()
- var sum = 0
- val largerMap = getLargerMap(map1, map2)
- for(i <- 0 until largerMap.values.toList.length) {
- // same size maps
- if (map1.contains(i) && map2.contains(i)) {
- sum = map1(i) + map2(i)
- map += (i -> sum)
- }
- if (map1.contains(i) && !map2.contains(i)) {
- sum = map1(i)
- map += (i -> sum)
- }
- if (!map1.contains(i) && map2.contains(i)) {
- sum = map2(i)
- map += (i -> sum)
- }
- }
- removeMapZeros(map)
- }
- println("vectorSum Function")
- println("======================================================")
- println(addVectors(map1, map2))
- println()
- println()
- println()
- // #10 Helper Method
- def getLargerMap(map1: Map[Int, Int], map2: Map[Int, Int]): Map[Int, Int] = {
- if (map1.values.toList.length > map2.values.toList.length) {
- map1
- } else {
- map2
- }
- }
- // #10 Helper Method
- def removeMapZeros(map: collection.mutable.Map[Int, Int]) : collection.mutable.Map[Int, Int] = {
- for (i <- 0 until map.toList.length) {
- if (map(i) == 0) {
- map.remove(i)
- }
- }
- map
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement