Skip to content

Commit 80491b1

Browse files
authored
Merge pull request #8 from mariotti/javascalawork
Javascalawork
2 parents 499c0a5 + e7ce0e0 commit 80491b1

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

java_questions/Q3_isRotated.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#From: http://stackoverflow.com/questions/2553522/interview-question-check-if-one-string-is-a-rotation-of-other-string
2+
# Answer from: comunity wiki
3+
# a litteral copy
4+
# Just a java started
5+
#
6+
#algorithm checkRotation(string s1, string s2)
7+
# if( len(s1) != len(s2))
8+
# return false
9+
# if( substring(s2,concat(s1,s1))
10+
# return true
11+
# return false
12+
#end
13+
#
14+
15+
boolean isRotation(String s1,String s2) {
16+
return (s1.length() == s2.length()) && ((s1+s1).indexOf(s2) != -1);
17+
}
18+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
# This is almost a copy paste from:
3+
# http://stackoverflow.com/questions/13878104/finding-the-most-frequent-common-element-in-a-collection
4+
# which is in principle under the MIT licence, See for example the discussion here:
5+
# https://meta.stackexchange.com/questions/271080/the-mit-license-clarity-on-using-code-on-stack-overflow-and-stack-exchange
6+
#
7+
*/
8+
object MostFrequentInt extends App {
9+
val lista = List(0, 1, 3, 2, 23, 43, 2, 1, 5, 2, 5, 2, 56, 23, 0, -1, 2, 2)
10+
val listb = List(4, 0, 1, 0, 4, 4, 4, 4, 0, 1, 1, 1,-6, 7)
11+
val listc = List(3, 5, 10, 3, 5, 1, 5, 3, 2, 3, 8, 4, 5, 1, 2, 7, 4, 6, 5, 8, 1, 1, 7, 10, 4, 5, 10, 5, 9, 1, 2, 10, 3, 9, 5, 10, 6, 10, 10, 8, 7, 2, 10, 7, 8, 10, 10, 7, 5, 10)
12+
println("Start")
13+
println(lista.groupBy(identity).maxBy(_._2.size)._1)
14+
println(listb.groupBy(identity).maxBy(_._2.size)._1)
15+
println(listc.groupBy(identity).maxBy(_._2.size)._1)
16+
}
17+
18+
/*
19+
As I am new to scala, to understand the code above... if you know python and/or java and/or perl...
20+
groupBy returns a Map (hashkey, dictionary, or similar) like:
21+
22+
scala> lista.groupBy(identity)
23+
res3: scala.collection.immutable.Map[Int,List[Int]] = Map(0 -> List(0, 0), 5 -> List(5, 5), 56 -> List(56), 1 -> List(1, 1), 2 -> List(2, 2, 2, 2, 2, 2), 3 -> List(3), -1 -> List(-1), 43 -> List(43), 23 -> List(23, 23))
24+
25+
MaxBy argument means: _ get the coming objects (the Map),
26+
_._2 use the second argument (the List),
27+
_._2.size of it consider the size and find the max.
28+
29+
30+
The REPL seems to be one of the nice things of scala ;)
31+
32+
*/
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Adapted from the tutorial at:
3+
http://docs.scala-lang.org/tutorials/tour/sequence-comprehensions.html
4+
*/
5+
6+
object PairsEqualToTen extends App {
7+
val lista = List(0, 1, 3, 2, 23, 43, 2, 1, 5, 2, 5, 2, 56, 23, 0, -1, 2, 2)
8+
val listb = List(4, 0, 1, 0, 4, 4, 4, 4, 0, 1, 1, 1,-6, 7)
9+
val listc = List(3, 5, 10, 3, 5, 1, 5, 3, 2, 3, 8, 4, 5, 1, 2, 7, 4, 6, 5, 8, 1, 1, 7, 10, 4, 5, 10, 5, 9, 1, 2, 10, 3, 9, 5, 10, 6, 10, 10, 8, 7, 2, 10, 7, 8, 10, 10, 7, 5, 10)
10+
val listd = List(0, 1, 3, 2, 7, 43, 2, 1, 5, 2, 5, 2, 8, 23, 0, -1, 2, 2)
11+
val liste = List(0, 1, 3, 10, 9, 7, 5, 5)
12+
13+
def foo(lst: List[Int], v: Int) =
14+
for ((i, idx) <- lst.view.zipWithIndex;
15+
j <- lst.slice(idx+1, lst.size) if i + j == v) yield{
16+
//Console.println(i, j, idx, lst.slice(idx+1, lst.size))
17+
(i, j)
18+
}
19+
20+
Console.println(lista)
21+
foo(lista, 10) foreach {
22+
case (i, j) =>
23+
println(s"($i, $j)")
24+
}
25+
26+
Console.println(listb)
27+
foo(listb, 10) foreach {
28+
case (i, j) =>
29+
println(s"($i, $j)")
30+
}
31+
32+
Console.println(listc)
33+
foo(listc, 10) foreach {
34+
case (i, j) =>
35+
println(s"($i, $j)")
36+
}
37+
38+
Console.println(listd)
39+
foo(listd, 10) foreach {
40+
case (i, j) =>
41+
println(s"($i, $j)")
42+
}
43+
44+
Console.println(liste)
45+
foo(liste, 10) foreach {
46+
case (i, j) =>
47+
println(s"($i, $j)")
48+
}
49+
50+
51+
}
52+

0 commit comments

Comments
 (0)