Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:jelmstrom/sevenlanguages_at_avega

  • Loading branch information...
commit 2fce52ed9ec2aea0bc7f1114421d03a04ba9f7ef 2 parents c0c41bf + 07f9662
@jelmstrom authored
View
9 README
@@ -1,9 +0,0 @@
-7 languages in 7 weeks @ Avega
-
-
-Här kan du dela med dig av din kod och få inspiration av andra.
-Lägg upp koden under det språk det tillhör och gör det lätt att förstå vilket problem det är relaterat till
-
-ex
-/[språk]/dag_1/gissa_nummer.rb
-
View
18 prolog/day_one/coloring.pl
@@ -0,0 +1,18 @@
+adjacent(1,2).
+adjacent(1,3).
+adjacent(1,4).
+adjacent(1,5).
+adjacent(2,3).
+adjacent(2,4).
+adjacent(3,4).
+adjacent(4,5).
+ adjacent(2,1).
+ adjacent(3,1).
+ adjacent(4,1).
+ adjacent(5,1).
+ adjacent(3,2).
+ adjacent(4,2).
+ adjacent(4,3).
+ adjacent(5,4).
+
+ adjacent(2,3).
View
10 prolog/day_one/food_ma.pl
@@ -0,0 +1,10 @@
+food_type(velveeta, cheese).
+food_type(ritz, cracker).
+food_type(spam, meat).
+food_type(sausage, meat).
+food_type(jolt, soda).
+food_type(twinkie, dessert).
+flavor(sweet, dessert).
+flavor(savory, meat).
+flavor(savory, cheese).
+flavor(sweet, soda).
View
15 prolog/day_one/map_ma.pl
@@ -0,0 +1,15 @@
+different(red, green). different(red, blue).
+different(green, red). different(green, blue).
+different(blue, red). different(blue, green).
+coloring(Alabama, Mississippi, Georgia, Tennessee, Florida) :-
+different(Mississippi, Tennessee),
+different(Mississippi, Alabama),
+different(Alabama, Tennessee),
+different(Alabama, Mississippi),
+different(Alabama, Georgia),
+different(Alabama, Florida),
+different(Georgia, Florida),
+different(Georgia, Tennessee).
+different(Canada, Tennessee).
+
+
View
44 prolog/day_three/sudoku6_ma.pl
@@ -0,0 +1,44 @@
+valid([]).
+valid([Head|Tail]) :-
+
+fd_all_different(Head),
+valid(Tail).
+
+sudoku(Puzzle, Solution) :-
+ Solution = Puzzle,
+Puzzle = [
+S11, S12, S13, S14,S15,S16,
+S21, S22, S23, S24,S25,S26,
+S31, S32, S33, S34,S35, S36,
+S41, S42, S43, S44, S45, S46,
+S51, S52, S53, S54, S55, S56,
+S61, S62, S63, S64, S65, S66
+],
+fd_domain(Solution, 1, 6),
+
+Row1 = [S11, S12, S13, S14, S15, S16],
+Row2 = [S21, S22, S23, S24, S25, S26],
+Row3 = [S31, S32, S33, S34, S35, S36],
+Row4 = [S41, S42, S43, S44, S45, S46],
+Row5 = [S51, S52, S53, S54, S55, S56],
+Row6 = [S61, S62, S63, S64, S65, S66],
+
+Col1 = [S11, S21, S31, S41, S51, S61],
+Col2 = [S12, S22, S32, S42, S52, S62],
+Col3 = [S13, S23, S33, S43, S53, S63],
+Col4 = [S14, S24, S34, S44, S54, S64],
+Col5 = [S15, S25, S35, S45, S55, S65],
+Col6 = [S16, S26, S36, S46, S56, S66],
+
+Square1 = [S11, S12, S13, S21, S22, S23],
+Square2 = [S14, S15, S16, S24, S25, S26],
+Square3 = [S31, S32, S33, S41, S42, S43],
+Square4 = [S34, S35, S36, S44, S45, S46],
+Square5 = [S51, S52, S53, S61, S62, S63],
+Square6 = [S54, S55, S56, S64, S65, S66],
+
+valid([Row1, Row2, Row3, Row4, Row5,Row6,
+Col1, Col2, Col3, Col4, Col5, Col6,
+Square1, Square2, Square3, Square4, Square5, Square6 ]).
+
+ sudoku([1, _, 2, 3,_, _, _, _,_, _, _, _,_, 4, _, _,1, _, 2, 3,_, _, _, _,_, _, _, _,_, 4, _, _,_,_,_,_],Solution).
View
28 prolog/day_three/sudoku_ma.pl
@@ -0,0 +1,28 @@
+valid([]).
+valid([Head|Tail]) :-
+fd_all_different(Head),
+valid(Tail).
+sudoku(Puzzle, Solution) :-
+Solution = Puzzle,
+Puzzle = [S11, S12, S13, S14,
+S21, S22, S23, S24,
+S31, S32, S33, S34,
+S41, S42, S43, S44],
+fd_domain(Solution, 1, 4),
+Row1 = [S11, S12, S13, S14],
+Row2 = [S21, S22, S23, S24],
+Row3 = [S31, S32, S33, S34],
+Row4 = [S41, S42, S43, S44],
+Col1 = [S11, S21, S31, S41],
+Col2 = [S12, S22, S32, S42],
+Col3 = [S13, S23, S33, S43],
+Col4 = [S14, S24, S34, S44],
+Square1 = [S11, S12, S21, S22],
+Square2 = [S13, S14, S23, S24],
+Square3 = [S31, S32, S41, S42],
+Square4 = [S33, S34, S43, S44],
+valid([Row1, Row2, Row3, Row4,
+Col1, Col2, Col3, Col4,
+Square1, Square2, Square3, Square4]).
+
+ sudoku([_, _, 2, 3,_, _, _, _,_, _, _, _,3, 4, _, _],Solution).
View
4 prolog/day_two/concat_ma.pl
@@ -0,0 +1,4 @@
+
+concatenate([], List, List).
+concatenate([Head|Tail1], List, [Head|Tail2]) :-
+concatenate(Tail1, List, Tail2)
View
4 prolog/day_two/fib_ma.pl
@@ -0,0 +1,4 @@
+fib(0,0).
+fib(1,1).
+fib(N,FIB):-
+ N>1,N1 is N-1, N2 is N -2, fib((N1),X) , fib(N2,Y), FIB is X+Y.
View
4 prolog/day_two/last_ma.pl
@@ -0,0 +1,4 @@
+last([],[]).
+last([Head|[TailHead|TailTail]],Last):-
+last(TailTail,Last),TailHead.
+
View
5 prolog/day_two/list_math_ma.pl
@@ -0,0 +1,5 @@
+count(0, []).
+count(Count, [Head|Tail]) :- count(TailCount, Tail), Count is TailCount + 1.
+sum(0, []).
+sum(Total, [Head|Tail]) :- sum(Sum, Tail), Total is Head + Sum.
+average(Average, List) :- sum(Sum, List), count(Count, List), Average is Sum/Count.
View
5 prolog/day_two/md_list_minimum.pl
@@ -0,0 +1,5 @@
+minimum(Old, New, Min) :- Old < New, Min is Old.
+minimum(Old, New, Min) :- New < Old, Min is New.
+
+list_min([], 1000000).
+list_min([Head|Tail], Min) :- list_min(Tail, OldMin), minimum(OldMin, Head, Min).
View
12 prolog/day_two/reverse_ma.pl
@@ -0,0 +1,12 @@
+
+reverseA([],List, List).
+reverseA([Head|Tail],Temp,Ny):-
+ reverseA(Tail,[Head, Temp],Ny).
+
+ rev([], Rev, Rev).
+rev([Head|Tail],Temp,Rev) :- rev(Tail,[Head|Temp], Rev).
+
+
+reverseB([],[]).
+reverseB([Head|Tail],[List,Head]) :-
+reverseB(Tail,List).
View
15 prolog/day_two/sort_ma.pl
@@ -0,0 +1,15 @@
+sortera([],[]).
+sortera([A],[A]).
+sortera([A,B|TAIL],AB):-
+newList(A,B,C),sortera(TAIL,C)
+
+newList(A,B,C):-
+( A =< B
+ -> C = [C, A,B]
+ ; C = [C, B,A]
+ ).
+newList(A,B,[]):-
+( A =< B
+ -> C = [A,B]
+ ; C = [B,A]
+ ).
View
45 scala/day_one/md_tictactoe/TicTacToe.scala
@@ -0,0 +1,45 @@
+object TicTacToe extends Application {
+
+ def assertResult(result: Result) = result match {
+ case Xwins() => println("X is the winner")
+ case Owins() => println("0 is the winner")
+ case Draw() => println("Its a draw")
+ case NotFinished() => println("Can't decide winner, not finished")
+ }
+
+// val board = Board
+
+// var result: Result = board.evaluateWinner()
+// assertResult(result)
+// board.placeMarker(Xmarker(), Coordinate(1, 1))
+// board.placeMarker(Xmarker(), Coordinate(1, 2))
+// assertResult(board.evaluateWinner())
+// board.placeMarker(Xmarker(), Coordinate(1, 3))
+// assertResult(board.evaluateWinner())
+
+ println("Game 2 xxxxxxxxxxxxxxxx")
+ val board2 = Board
+ board2.placeMarker(Xmarker(), Coordinate(1, 1))
+ board2.placeMarker(Xmarker(), Coordinate(1, 2))
+ board2.placeMarker(Omarker(), Coordinate(1, 3))
+ board2.placeMarker(Omarker(), Coordinate(2, 1))
+ board2.placeMarker(Omarker(), Coordinate(2, 2))
+ board2.placeMarker(Xmarker(), Coordinate(2, 3))
+ board2.placeMarker(Xmarker(), Coordinate(3, 1))
+ board2.placeMarker(Xmarker(), Coordinate(3, 2))
+ board2.placeMarker(Omarker(), Coordinate(3, 3))
+
+ assertResult(board2.evaluateWinner())
+}
+
+abstract class Result()
+
+case class Winner() extends Result
+
+case class Xwins() extends Winner
+
+case class Owins() extends Winner
+
+case class Draw() extends Result
+
+case class NotFinished() extends Result
View
75 scala/day_one/md_tictactoe/md_ticTackToe.scala
@@ -0,0 +1,75 @@
+import collection.immutable.HashMap
+
+case class Coordinate(row: Int, col: Int)
+
+abstract case class Marker()
+
+case class Xmarker() extends Marker
+
+case class Omarker() extends Marker
+
+object Board {
+ val row1 = List(Coordinate(1, 1), Coordinate(1, 2), Coordinate(1, 3))
+ val row2 = List(Coordinate(1, 1), Coordinate(1, 2), Coordinate(1, 3))
+ val row3 = List(Coordinate(1, 1), Coordinate(1, 2), Coordinate(1, 3))
+ val col1 = List(Coordinate(1, 1), Coordinate(2, 1), Coordinate(3, 1))
+ val col2 = List(Coordinate(1, 2), Coordinate(2, 2), Coordinate(3, 2))
+ val col3 = List(Coordinate(1, 3), Coordinate(2, 3), Coordinate(3, 3))
+ val cross1 = List(Coordinate(1, 1), Coordinate(2, 2), Coordinate(3, 3))
+ val cross2 = List(Coordinate(1, 3), Coordinate(2, 2), Coordinate(3, 1))
+ val possibleSolutions = List(row1, row2, row3, col1, col2, col3, cross1, cross2)
+ var placedMarkers = new HashMap[Coordinate, Marker]
+
+ def placeMarker(marker: Marker, position: Coordinate) {
+ placedMarkers += position -> marker
+ println("Placed marker: " + placedMarkers(position))
+ }
+
+ def evaluateWinner(): Result = {
+ var result: Result = null
+ possibleSolutions.foreach {
+ possibleSolution =>
+ result = evaluatePossibleWinner(possibleSolution)
+ result match {
+ case Winner() => {
+ println("Winner detected")
+ return result
+ }
+ case _ => println("No winner yet, continue")
+ }
+ }
+ result match {
+ case NotFinished() => new Draw
+ }
+ }
+
+ def evaluatePossibleWinner(possibleWinner: List[Coordinate]): Result = {
+ var numberOfXInRow = 0
+ var numberOfOInRow = 0
+ var numberOfEmptyInRow = 0
+ possibleWinner.foreach {
+ position: Coordinate =>
+ var marker: Marker = null
+ if (placedMarkers.contains(position)) {
+ marker = placedMarkers(position)
+ }
+ marker match {
+ case Xmarker() => numberOfXInRow += 1
+ case Omarker() => numberOfOInRow += 1
+ case null => numberOfEmptyInRow += 1
+ }
+
+ }
+ println("Number of x in solution: " + numberOfXInRow)
+ println("Number of o in solution: " + numberOfOInRow)
+ println("Number of empty solution: " + numberOfEmptyInRow)
+ var res: Result = new NotFinished
+ if (numberOfXInRow == 3) {
+ res = new Xwins
+ } else if (numberOfOInRow == 3) {
+ res = new Owins
+ }
+ res
+ }
+
+}
View
10 scala/day_two/md_censor/Censor.scala
@@ -0,0 +1,10 @@
+import collection.immutable.HashMap
+
+trait Censor {
+ val wordReplacements = HashMap("Shoot" -> "Pucky", "Darn" -> "Beans")
+
+ def replaceWords(text: String): String = {
+ val censoredText : String = wordReplacements.foldLeft(text)((text, replacement) => text.replace(replacement._1, replacement._2))
+ censoredText
+ }
+}
View
8 scala/day_two/md_censor/CurseCensor.scala
@@ -0,0 +1,8 @@
+
+object CurseCensor extends Application with Censor {
+
+ val textWithCurse = "Shoot and Darn now I am made Shoot again"
+ val textWithoutCurse = replaceWords(textWithCurse)
+ println(textWithoutCurse)
+
+}
View
10 scala/day_two/md_fileBasedDictionary/FileBasedDictionary.scala
@@ -0,0 +1,10 @@
+import collection.mutable.HashMap
+import io.Source
+
+object FileBasedDictionary extends Application {
+
+ val file = Source.fromFile("day_two/md_fileBasedDictionary/curseDictionary.txt").getLines()
+ val censorDictionary = new HashMap[String, String]
+ file.foreach(line => censorDictionary += line.split(" ")(0) -> line.split(" ")(1))
+ censorDictionary.foreach(entry => println(entry))
+}
View
2  scala/day_two/md_fileBasedDictionary/curseDictionary.txt
@@ -0,0 +1,2 @@
+Bad Good
+Orange Apple
View
6 scala/day_two/md_foldleft/SizeOfStringList.scala
@@ -0,0 +1,6 @@
+package md_foldleft
+
+object SizeOfStringList extends Application {
+ val list = List("a", "b", "c")
+ list.foldLeft(0)((sum, value) => sum + 1)
+}
Please sign in to comment.
Something went wrong with that request. Please try again.