From 63fb232da613d50a6b28166cec620d3738f24e20 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sun, 4 Sep 2022 19:20:13 +0100 Subject: - Added more guest contributions by Laurent Rosenfeld. --- challenge-180/laurent-rosenfeld/d/ch-1.d | 19 ++++++++++++++++ challenge-180/laurent-rosenfeld/javascript/ch-1.js | 15 +++++++++++++ challenge-180/laurent-rosenfeld/kotlin/ch-1.kt | 16 +++++++++++++ challenge-180/laurent-rosenfeld/lua/ch-1.lua | 15 +++++++++++++ challenge-180/laurent-rosenfeld/python/ch-1.py | 9 ++++++++ challenge-180/laurent-rosenfeld/ring/ch-1.ring | 12 ++++++++++ challenge-180/laurent-rosenfeld/ruby/ch-1.rb | 14 ++++++++++++ challenge-180/laurent-rosenfeld/scala/ch-1.scala | 26 ++++++++++++++++++++++ 8 files changed, 126 insertions(+) create mode 100644 challenge-180/laurent-rosenfeld/d/ch-1.d create mode 100644 challenge-180/laurent-rosenfeld/javascript/ch-1.js create mode 100644 challenge-180/laurent-rosenfeld/kotlin/ch-1.kt create mode 100644 challenge-180/laurent-rosenfeld/lua/ch-1.lua create mode 100644 challenge-180/laurent-rosenfeld/python/ch-1.py create mode 100644 challenge-180/laurent-rosenfeld/ring/ch-1.ring create mode 100644 challenge-180/laurent-rosenfeld/ruby/ch-1.rb create mode 100644 challenge-180/laurent-rosenfeld/scala/ch-1.scala diff --git a/challenge-180/laurent-rosenfeld/d/ch-1.d b/challenge-180/laurent-rosenfeld/d/ch-1.d new file mode 100644 index 0000000000..cd15175b9e --- /dev/null +++ b/challenge-180/laurent-rosenfeld/d/ch-1.d @@ -0,0 +1,19 @@ +import std.stdio; +import std.array; + +void main() { + string[] tests = [ "Perl Weekly Challenge", "Long Live Perl" ]; + foreach(test; tests) { + int[string] histo; + string[] chars = test.split(""); + foreach (ch; chars) { + histo[ch]++; + } + for (int i = 0; i < chars.length; i++) { + if (histo[chars[i]] == 1) { + writeln(test, ": ", i); + break; + } + } + } +} diff --git a/challenge-180/laurent-rosenfeld/javascript/ch-1.js b/challenge-180/laurent-rosenfeld/javascript/ch-1.js new file mode 100644 index 0000000000..59b96a91f1 --- /dev/null +++ b/challenge-180/laurent-rosenfeld/javascript/ch-1.js @@ -0,0 +1,15 @@ +let tests = [ "Perl Weekly Challenge", "Long Live Perl" ] +for (let i = 0; i < tests.length; i++) { + test = tests[i] + histo = new Map(); + chars = test.split("") + chars.forEach( char => { + histo.set(char, histo.has(char) ? histo.get(char) + 1 : 1) + }) + for (let i = 0; i < chars.length; i++) { + if (histo.get(chars[i]) == 1) { + console.log(test, ": ", i) + break + } + } +} diff --git a/challenge-180/laurent-rosenfeld/kotlin/ch-1.kt b/challenge-180/laurent-rosenfeld/kotlin/ch-1.kt new file mode 100644 index 0000000000..419df6e3de --- /dev/null +++ b/challenge-180/laurent-rosenfeld/kotlin/ch-1.kt @@ -0,0 +1,16 @@ +fun main() { + val tests = arrayOf("Perl Weekly Challenge", "Long Live Perl") + for (test in tests) { + var his = mutableMapOf() // letter histogram + val chars = test.toCharArray() + for (ch in chars) { + his[ch] = if (his.containsKey(ch)) his[ch]!! + 1 else 1 + } + for (i in 0..chars.size) { + if (his[chars[i]] == 1) { + println(test + ": " + i) + break + } + } + } +} diff --git a/challenge-180/laurent-rosenfeld/lua/ch-1.lua b/challenge-180/laurent-rosenfeld/lua/ch-1.lua new file mode 100644 index 0000000000..b64459a792 --- /dev/null +++ b/challenge-180/laurent-rosenfeld/lua/ch-1.lua @@ -0,0 +1,15 @@ +for _, test in pairs{"Perl Weekly Challenge", "Long Live Perl"} do + local histo = {} -- letter histotogram + for ch in string.gmatch(test, ".") do + -- histo[ch] = histo[ch] == nil and 1 or histo[ch] + 1 + histo[ch] = (histo[ch] or 0) + 1 + end + i = 0 + for ch in string.gmatch(test, ".") do + if histo[ch] == 1 then + print(test, ": ", i) + break + end + i = i + 1 + end +end diff --git a/challenge-180/laurent-rosenfeld/python/ch-1.py b/challenge-180/laurent-rosenfeld/python/ch-1.py new file mode 100644 index 0000000000..254e6a04e5 --- /dev/null +++ b/challenge-180/laurent-rosenfeld/python/ch-1.py @@ -0,0 +1,9 @@ +"""Find the first unique character in the given string""" +for test in ["Perl Weekly Challenge", "Long Live Perl"]: + histo = dict() + for char in test: + histo[char] = histo.get(char, 0) + 1 + for i in range(0, len(test)): + if histo[test[i]] == 1: + print(test, ": ", i) + break diff --git a/challenge-180/laurent-rosenfeld/ring/ch-1.ring b/challenge-180/laurent-rosenfeld/ring/ch-1.ring new file mode 100644 index 0000000000..f9ffda0dbc --- /dev/null +++ b/challenge-180/laurent-rosenfeld/ring/ch-1.ring @@ -0,0 +1,12 @@ +for test in [ "Perl Weekly Challenge", "Long Live Perl" ] + histo = [] + for i = 1 to len(test) + histo[test[i]] = 0 + histo[test[i]] + 1 + next + for i = 1 to len(test) + if histo[test[i]] = 1 + see test + ": " + (i-1) + nl + exit + ok + next +next diff --git a/challenge-180/laurent-rosenfeld/ruby/ch-1.rb b/challenge-180/laurent-rosenfeld/ruby/ch-1.rb new file mode 100644 index 0000000000..a2d0d9d033 --- /dev/null +++ b/challenge-180/laurent-rosenfeld/ruby/ch-1.rb @@ -0,0 +1,14 @@ +for test in ["Perl Weekly Challenge", "Long Live Perl"] + chars = test.split("") + # with new(0), 0 becomes the default value when a key is absent + histo = Hash.new(0) + for char in chars + histo[char] += 1 + end + for i in 0..chars.length - 1 + if histo[chars[i]] == 1 + print test, ": ", i, "\n" + break + end + end +end diff --git a/challenge-180/laurent-rosenfeld/scala/ch-1.scala b/challenge-180/laurent-rosenfeld/scala/ch-1.scala new file mode 100644 index 0000000000..5e3155f976 --- /dev/null +++ b/challenge-180/laurent-rosenfeld/scala/ch-1.scala @@ -0,0 +1,26 @@ +object firstUnique extends App { + import scala.collection.mutable.Map + val tests: List[String] = + List("Perl Weekly Challenge", "Long Live Perl") + for (test <- tests) { + val chars = test.split("") + var histo: Map[String, Int] = Map() + for (ch <- chars) { + if (histo.contains(ch)) { + histo(ch) = histo(ch) + 1 + } else { + histo += (ch -> 1) + } + } + var continue = true + var i = 0 + while (continue) { + if (histo(chars(i)) == 1) { + println(test + ": " + i) + continue = false + + } + i = i + 1 + } + } +} -- cgit