diff --git a/README.md b/README.md index 1a0189b7c..3f105bf55 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,11 @@ Checked implementations are written in scala. If not, those are not converted to - [array](src/main/scala/algorithms/array) - [x] [CircularCounter](src/main/scala/algorithms/array/CircularCounter.scala) - [x] [Flatten](src/main/scala/algorithms/array/Flatten.scala) - - [x] [Garage](src/main/scala/algorithms/array/Garage.scala) - - [x] [LongestNonRepeat](src/main/scala/algorithms/array/LongestNonRepeat.scala) - - [x] [MergeIntervals](src/main/scala/algorithms/array/MergeIntervals.scala) + - [x] [garage](src/main/scala/algorithms/array/Garage.scala) + - [longest_non_repeat](python/array/longest_non_repeat.py/) + - [merge_intervals](python/array/merge_intervals.py) - [x] [MissingRanges](src/main/scala/algorithms/array/MissingRanges.scala) - - [plus_one](python/array/plus_one.py) + - [x] [plus_one](src/main/scala/algorithms/array/PlusOne.scala) - [x] [RotateArray](src/main/scala/algorithms/array/RotateArray.scala) - [summary_ranges](python/array/summary_ranges.py) - [x] [ThreeSum](src/main/scala/algorithms/array/ThreeSum.scala) @@ -39,9 +39,7 @@ Checked implementations are written in scala. If not, those are not converted to - [shortest_distance_from_all_buildings](python/bfs/shortest_distance_from_all_buildings.py) - [word_ladder](python/bfs/word_ladder.py) - [bit](python/bit) - - [bytes_int_conversion](pytho/bit/bytes_int_conversion.py) - [count_ones](python/bit/count_ones.py) - - [find_missing_number](python/bit/find_missing_number.py) - [power_of_two](python/bit/power_of_two.py) - [reverse_bits](python/bit/reverse_bits.py) - [single_number2](python/bit/single_number2.py) @@ -141,7 +139,7 @@ Checked implementations are written in scala. If not, those are not converted to - [stack](python/stack/stack.py) - [valid_parenthesis](python/stack/valid_parenthesis.py) - [string](python/string) - - [add_binary](python/string/add_binary.py) + - [x] [add_binary](src/main/scala/algorithms/string/AddBinary.scala) - [breaking_bad](python/string/breaking_bad.py) - [decode_string](python/string/decode_string.py) - [encode_decode](python/string/encode_decode.py) diff --git a/python/array/plus_one.py b/python/array/plus_one.py deleted file mode 100644 index 8d47b0827..000000000 --- a/python/array/plus_one.py +++ /dev/null @@ -1,38 +0,0 @@ -# Given a non-negative number represented as an array of digits, -# plus one to the number. - -# The digits are stored such that the most significant -# digit is at the head of the list. - - -def plusOne(digits): - """ - :type digits: List[int] - :rtype: List[int] - """ - digits[-1] = digits[-1] + 1 - res = [] - ten = 0 - i = len(digits)-1 - while i >= 0 or ten == 1: - sum = 0 - if i >= 0: - sum += digits[i] - if ten: - sum += 1 - res.append(sum % 10) - ten = sum / 10 - i -= 1 - return res[::-1] - - -def plus_one(digits): - n = len(digits) - for i in range(n-1, -1, -1): - if digits[i] < 9: - digits[i] += 1 - return digits - digits[i] = 0 - new_num = [0] * (n+1) - new_num[0] = 1 - return new_num diff --git a/src/main/scala/algorithms/array/Garage.scala b/src/main/scala/algorithms/array/Garage.scala index 393b1456b..34f00b7c6 100644 --- a/src/main/scala/algorithms/array/Garage.scala +++ b/src/main/scala/algorithms/array/Garage.scala @@ -1,51 +1,91 @@ package algorithms.array +import scala.collection.mutable + /** - * There is a parking lot with only one empty spot. Given the initial state - * of the parking lot and the final state. Each step we are only allowed to - * move a car - * out of its place and move it into the empty spot. - * The goal is to find out the least movement needed to rearrange - * the parking lot from the initial state to the final state. - * - * Say the initial state is an array: - * - * [1,2,3,0,4], - * where 1,2,3,4 are different cars, and 0 is the empty spot. - * - * And the final state is - * - * [0,3,2,1,4]. - * We can swap 1 with 0 in the initial array to get [0,2,3,1,4] and so on. - * Each step swap with 0 only. + * # There is a parking lot with only one empty spot. Given the initial state +*# of the parking lot and the final state. Each step we are only allowed to +*# move a car +*# out of its place and move it into the empty spot. +*# The goal is to find out the least movement needed to rearrange +*# the parking lot from the initial state to the final state. + ** + # Say the initial state is an array: + ** + # [1,2,3,0,4], +*# where 1,2,3,4 are different cars, and 0 is the empty spot. + ** + # And the final state is + ** + # [0,3,2,1,4]. +*# We can swap 1 with 0 in the initial array to get [0,2,3,1,4] and so on. +*# Each step swap with 0 only. + ** + Example + *input : 1, 2, 3, 0, 4 + *final : 0, 3, 2, 1, 4 + ** + answer : 4 steps + ** + step 1 : 0, 2, 3, 1, 4 + *step 2 : 2, 0, 3, 1, 4 // 없으면 0 제외한 가장 앞에 것과 swap + *step 3 : 2, 3, 0, 1, 3 + *step 4 : 0, 3, 2, 1, 4 +*site : https://discuss.leetcode.com/topic/67928/rearrange-parking-lot +*site : https://www.careercup.com/question?id=5687257423937536 + * + * User: before30 + * Date: 2017. 5. 9. + * Time: PM 8:49 */ object Garage { - def apply(init: List[Int], fin: List[Int]): Int = { - if (init.size != fin.size) throw new IllegalArgumentException("Size of initial and final state is not equal") - - var i = 0 - var move = 0 - var list = init.drop(0) - - while (list != fin) { - val v = list.apply(i) - val f = fin.apply(i) + def swap(current: List[Int], emptyIdx: Int, targetIdx: Int): List[Int] = { + current.updated(emptyIdx, current(targetIdx)).updated(targetIdx, current(emptyIdx)) + } - if (v != 0 && v != f) { - list = list.updated(list.indexOf(0), v).updated(i, 0) - move += 1 + def isFinished(currentGarage: List[Int], finalGarage: List[Int]): Boolean = { + currentGarage == finalGarage + } - if (list.indexOf(v) != fin.indexOf(v)) { - list = list.updated(list.indexOf(v), list.apply(fin.indexOf(v))).updated(fin.indexOf(v), v) - move += 1 + def calc(initGarage: scala.collection.mutable.MutableList[Int], desigredGarage: List[Int]): Int = { + var step = 0 + for ( i <- 0 until initGarage.length ) { + if (initGarage(i) != desigredGarage(i) + && desigredGarage(i) != 0) { + val indexOfZero = initGarage.indexOf(0) + if (i != indexOfZero) { + val temp = initGarage(i) + initGarage(i) = 0 + initGarage(indexOfZero) = temp + step = step + 1 } + val indexOfTarget = initGarage.indexOf(desigredGarage(i)) + initGarage(i) = initGarage(indexOfTarget) + initGarage(indexOfTarget) = 0 + step = step + 1 } - - i = (i + 1) % init.size } - move + step } + def getFirstDeprecntExceptZero(currentGarage: List[Int], finalGarage: List[Int]): Either[String, Int] = { + + Left("nothing") + } + + def main(args: Array[String]): Unit = { +// println(swap(List(1,2,3,4,0), 4, 0)) +// println(isFinished(List(1, 2, 3, 4, 5), List(1, 2, 3, 4, 5))) +// println(isFinished(List(1,2,3,4,5), List(2,1,3,4,5))) + +// println(calc(List(1,2,3,0), List(1,2,3,0))) + /* + input : 1, 2, 3, 0, 4 + final : 0, 3, 2, 1, 4 + + */ + println(calc(mutable.MutableList(1, 2, 3, 0, 4), List(0, 3, 2, 1, 4))) + } } diff --git a/src/main/scala/algorithms/array/PlusOne.scala b/src/main/scala/algorithms/array/PlusOne.scala new file mode 100644 index 000000000..d60d92027 --- /dev/null +++ b/src/main/scala/algorithms/array/PlusOne.scala @@ -0,0 +1,52 @@ +package algorithms.array + +import scala.collection.immutable.Stream.Empty + +/** + * User: before30 + * Date: 2017. 5. 8. + * Time: AM 8:59 + * + * Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. + * You may assume the integer do not contain any leading zero, except the number 0 itself. + * The digits are stored such that the most significant digit is at the head of the list. + * + * Example + * f([1, 2, 3] : 123) = [1, 2, 4]: 124 + * f([9, 9, 9] : 999) = [1, 0, 0, 0]: 1000 + */ +object PlusOne { + + def apply(digits: List[Int], digit: Int): List[Int] = { + plusDigit(digits, digit) + } + + def apply(digits: List[Int]): List[Int] = { + plusOne(digits) + } + + def plusOne(digits: List[Int]): List[Int] = { + plusDigit(digits, 1) + } + + def plusDigit(digits: List[Int], digit: Int): List[Int] = { + val test = calc(digits.reverse, digit, Nil) + println(test) + test + } + + def calc(digits: List[Int], carry: Int, acc: List[Int]): List[Int] = { + digits match { + case Nil if carry == 0 => acc + case Nil if carry > 0 => carry::acc + case _ => { + val temp = digits.head + carry + calc(digits.tail, temp/10, temp%10::acc) + } + } + } + + def main(args: Array[String]): Unit = { + plusOne(List(1,2,3)) + } +} diff --git a/src/main/scala/algorithms/string/AddBinary.scala b/src/main/scala/algorithms/string/AddBinary.scala new file mode 100644 index 000000000..17f9e7ddc --- /dev/null +++ b/src/main/scala/algorithms/string/AddBinary.scala @@ -0,0 +1,48 @@ +package algorithms.string + +/** + * Created by before30 on 16/05/2017. + */ + +/* +Given two binary strings, +return their sum (also a binary string). + +For example, + a = "11" + b = "1" +Return "100". +*/ + +object AddBinary { + + // "1111", "1" -> 10000 + + def add(list1: List[Int], list2: List[Int], carry: Int, acc: List[Int]): List[Int] = { + if (list1.isEmpty && list2.isEmpty && carry != 0) carry::acc + else if(list1.isEmpty && list2.isEmpty && carry == 0) acc + else { + val (h1, t1) = if (!list1.isEmpty) (list1.head, list1.tail) else (0, Nil) + val (h2, t2) = if (!list2.isEmpty) (list2.head, list2.tail) else (0, Nil) + val c1 = (h1 + h2 + carry) / 2 + val v = (h1 + h2 + carry) % 2 + add(t1, t2, c1, v :: acc) + } + + + } + def apply(a: String, b: String): String = { + + add(a.toList.map(x => x - '0').reverse, b.toList.map(x => x - '0').reverse, 0, Nil).mkString("") + } + + def main(args: Array[String]): Unit = { + println("10000 == " + AddBinary("1111", "1")) + println("0 == " + AddBinary("0", "0")) + println("10 == " + AddBinary("1", "1")) + println("10000000001 == " + AddBinary("10000000000", "1")) + println("10000 == " + AddBinary("1101", "11")) + println("10000 ==" + AddBinary("1", "1111")) + println("101 ==" + AddBinary("001", "100")) + } +} diff --git a/src/test/scala/algorithms/array/GarageTest.scala b/src/test/scala/algorithms/array/GarageTest.scala index 37eec85da..e63180384 100644 --- a/src/test/scala/algorithms/array/GarageTest.scala +++ b/src/test/scala/algorithms/array/GarageTest.scala @@ -2,25 +2,25 @@ package algorithms.array import algorithms.UnitSpec - +/** + * User: before30 + * Date: 2017. 5. 9. + * Time: PM 11:19 + */ class GarageTest extends UnitSpec { - "Garage" when { - - "size of initial and final state is equal" should { - "success" in { - assert(Garage(List(1, 2, 3, 0, 4), List(0, 3, 2, 1, 4)) == 4) - } + "Method Test" should { + "swap test" in { + assert(Garage.swap(List(1,2,3,4,0), 4, 0) == List(0,2,3,4,1)) } - "size of initial and final state is not equal" should { - "throw IllegalArgumentException" in { - intercept[IllegalArgumentException] { - Garage(List(1, 2, 3, 0, 4), List(0, 3, 2, 1)) - } - } + "isFinished true" in { + assert(Garage.isFinished(List(1,2,3,4,5), List(1,2,3,4,5)) == true) } + "isFinished false" in { + assert(Garage.isFinished(List(1,2,3,4,5), List(1,2,3,5,4)) == false) + } } } diff --git a/src/test/scala/algorithms/array/LongestSubstring.scala b/src/test/scala/algorithms/array/LongestSubstring.scala new file mode 100644 index 000000000..25b3f305d --- /dev/null +++ b/src/test/scala/algorithms/array/LongestSubstring.scala @@ -0,0 +1,20 @@ +package algorithms.array + +/** + * User: before30 + * Date: 2017. 5. 10. + * Time: PM 12:02 + * + * Difficulty : Medium + * Given a string, find the length of the longest substring without repeating characters. + * + * Examples: + * Given "abcabcbb", the answer is "abc", which the length is 3. + * Given "bbbbb", the answer is "b", with the length of 1. + * Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. + * + * site : https://leetcode.com/problems/longest-substring-without-repeating-characters/#/description + */ +object LongestSubstring { + +} diff --git a/src/test/scala/algorithms/array/PlusOneTest.scala b/src/test/scala/algorithms/array/PlusOneTest.scala new file mode 100644 index 000000000..6ebf65750 --- /dev/null +++ b/src/test/scala/algorithms/array/PlusOneTest.scala @@ -0,0 +1,35 @@ +package algorithms.array + +import algorithms.UnitSpec + +/** + * User: before30 + * Date: 2017. 5. 8. + * Time: AM 9:16 + */ +class PlusOneTest extends UnitSpec { + "PlusOne" should { + + "success1" in { + assert(PlusOne(List(1, 2, 3)) == List(1, 2, 4)) + } + + "success2" in { + assert(PlusOne(List(9, 9, 9)) == List(1, 0, 0, 0)) + } + + } + + "PlusDigit" should { + + "success1" in { + assert(PlusOne(List(1, 2, 3), 2) == List(1, 2, 5)) + } + + "success2" in { + assert(PlusOne(List(9, 9, 9), 2) == List(1, 0, 0, 1)) + } + + } + +} diff --git a/src/test/scala/algorithms/string/AddBinaryTest.scala b/src/test/scala/algorithms/string/AddBinaryTest.scala new file mode 100644 index 000000000..eace306b1 --- /dev/null +++ b/src/test/scala/algorithms/string/AddBinaryTest.scala @@ -0,0 +1,36 @@ +package algorithms.string + +import algorithms.UnitSpec + +/** + * Created by before30 on 16/05/2017. + */ +class AddBinaryTest extends UnitSpec { + "AddBinary" when { + "1111, 1" in { + assert(AddBinary("1111", "1") == "10000") + } + + "0, 0" in { + assert(AddBinary("1111", "1") == "10000") + } + + "1, 1" in { + assert(AddBinary("1", "1") == "10") + } + + "1101, 11" in { + assert(AddBinary("1101", "11") == "10000") + } + + "1, 1111" in { + assert(AddBinary("1", "1111") == "10000") + + } + + "001, 100" in { + assert(AddBinary("001", "100") == "101") + } + } + +}