Skip to content

Instantly share code, notes, and snippets.

@DeepSky8
DeepSky8 / problem09.scala
Last active August 29, 2015 14:02
P09 (**) Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements they should be placed in separate sublists. Example: scala> pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) res0: List[List[Symbol]] = List(List('a, 'a, 'a, 'a), List('b), List('c, 'c), List('a, 'a), List('d), List('e, 'e,…
def pack[A](input: List[A]): List[List[A]] = input match {
case Nil => Nil
case h :: t => input.filter(_ == input.head) :: pack(packHelper(input.tail))
}
def packHelper[A](input: List[A]): List[A] = input match {
case Nil => Nil
case h :: t => input.filterNot(_ == input.head)
}
@DeepSky8
DeepSky8 / encode.scala
Created June 18, 2014 18:49
P10 (*) Run-length encoding of a list. Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as tuples (N, E) where N is the number of duplicates of the element E. Example: scala> encode(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) res…
def encode[A](input: List[A]): List[(Int, A)] = input match {
case Nil => Nil
case h :: t => (pack(input)).map(shorten)
}
def shorten[A](input: List[A]): List[(Int, A)] = input match {
case Nil => Nil
case h :: t => (input.length, h)
}
@DeepSky8
DeepSky8 / runLengthEncoder.scala
Created June 24, 2014 16:48
Run-length encoding of a list (direct solution). Implement the so-called run-length encoding data compression method directly. I.e. don't use other methods you've written (like P09's pack); do all the work directly. Example: scala> encodeDirect(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) res0: List[(Int, Symbol)] = List((4,'a),…
def encodeDirect[A](input: List[A]): List(Int, A) = input match {
case Nil => Nil
case h :: t => collapse(collate(input))
}
def collate(input: List[A]): List[A] = input match {
case Nil => Nil
case h :: t => if(t.headOption.map(_==h).getThisOr(false)) h :: collate(t) else list(h) :: collate(t)
}
@DeepSky8
DeepSky8 / removeAt.scala
Created July 2, 2014 17:57
P20 (*) Remove the Kth element from a list. Return the list and the removed element in a Tuple. Elements are numbered from 0. Example: scala> removeAt(1, List('a, 'b, 'c, 'd)) res0: (List[Symbol], Symbol) = (List('a, 'c, 'd),'b)
def removeAt[A](n: Int, input: List[A]): (List[A], A) = {
def removeAtHelper(input: List[A], p: Int = n): List[A] = input match{
case Nil => Nil
case h :: t => if(p > 0) h :: removeAtHelper(t, p - 1) else t
}
def symbol(input: List[A], q: Int): Option[A] = input match {
case Nil => Nothing
case h :: t => if(q > 0) symbol(t, q - 1) else t.headOption.getOrElse(Nothing)
}
(removeAtHelper(input), symbol(input))
@DeepSky8
DeepSky8 / treasureMap.scala
Created October 15, 2014 16:40
Playing around with maps
import java.io.{BufferedReader, InputStreamReader}
import scala.collection.mutable.Map
object Main {
def main(args: Array[String]) {
println("Welcome. What key shall we use?")
val key = readLine()
//val key2 = key + \n
println("And what shall it unlock?")
val unlock = readLine()
val treasureMap = Map[String, String]()
@DeepSky8
DeepSky8 / serverSocketPancakes.scala
Last active August 29, 2015 14:10
Server Socket Pancakes
object Listen {
import java.net._
import java.io._
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
def Listen(): Unit = {
val doorbell = new Thread(new Runnable {
@DeepSky8
DeepSky8 / variableTextServer.scala
Created January 4, 2015 16:36
The assignment is to create a server that will recognize different requests being submitted, and serve different results based on the request. This server currently requires a knowledge of the result location - the root directory only welcomes the browser with a welcome text. There are no hyperlinks for further information.
object Listen {
import java.net._
import java.io._
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
def Listen(): Unit = {
val doorbell = new Thread(new Runnable {
@DeepSky8
DeepSky8 / serverRecord.scala
Created January 20, 2015 14:33
Trying to handle different types of requests, and have a recorded compilation of the data that the browsers sent when creating the request
import java.net._
import java.io._
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File
import java.io.{ BufferedReader, InputStreamReader, File, FileInputStream, InputStream, OutputStream, FileOutputStream, FileReader }
import javax.swing.ImageIcon;
class Request(method: String, version: String, pathway: String) {
@DeepSky8
DeepSky8 / ServerParserAttempt.scala
Created February 3, 2015 18:57
Server Attempt that seeks to parse each line of browser information
import java.net._
import java.io._
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File
import java.io.{ BufferedReader, InputStreamReader, File, FileInputStream, InputStream, OutputStream, FileOutputStream, FileReader }
import javax.swing.ImageIcon;
import scala.util.parsing.combinator.{ Parsers, RegexParsers }
import scala.util.parsing.input._
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="file:///C:/Ongoing%20Projects/IssueTracker/cssLoginPage.css">
<title>Issue Tracker</title>
</head>
<body>
<h1>Welcome to Issue Tracker</h1>