-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw1.scala
53 lines (43 loc) · 1.42 KB
/
hw1.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
object hw1 extends hwtest.hw("CS384"):
def userName = "emily"
// DOCUMENTATION SIGNATURE BLOCK GOES HERE
//I DID NOT USE ANY SOURCES OR ASSISTANCE REQUIRING DOCUMENTATION IN COMPLETING THIS ASSIGNMENT.
// Visit the syllabus for more information:
// https://eecscourses.westpoint.edu/courses/cs384/#documentation
def numDigits(number: Int): Int =
var amount = 0
var number_var = number
while number_var > 10 do
number_var = number_var/10
amount += 1
amount
test("numDigits", numDigits, "number")
def firstOdd(array: Array[Int]): Option[Int] =
for i <- array.length do
if array(i) % 2 != 0 then
Some(array(i))
None
test("firstOdd", firstOdd, "array")
def maxChar(string: String): (Char,Int) =
var items = Array.fill(string.length)((' ',0))
var max = 0
var max_char = ' '
var counter = -1
for item <- string do
counter += 1
if item in items then
items(counter)(1) += 1
else
for item <- items do
if items(counter)(0) == ' ' then
items(counter)(0) = item
items(counter)(1) = 1
counter = -1
for element <- items do
counter += 1
if items(counter)(1) > max then
max = items(counter)(1)
max_char = items(counter)(0)
val final_tuple = (max_char, max)
final_tuple
test("maxChar", maxChar, "string")