aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-09-04 19:20:13 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-09-04 19:20:13 +0100
commit63fb232da613d50a6b28166cec620d3738f24e20 (patch)
tree3892d402274c10ba5580656e4c89cfe884462b8e
parentd4fb734fa6a97b50c3cf0655475fe3c34801ce55 (diff)
downloadperlweeklychallenge-club-63fb232da613d50a6b28166cec620d3738f24e20.tar.gz
perlweeklychallenge-club-63fb232da613d50a6b28166cec620d3738f24e20.tar.bz2
perlweeklychallenge-club-63fb232da613d50a6b28166cec620d3738f24e20.zip
- Added more guest contributions by Laurent Rosenfeld.
-rw-r--r--challenge-180/laurent-rosenfeld/d/ch-1.d19
-rw-r--r--challenge-180/laurent-rosenfeld/javascript/ch-1.js15
-rw-r--r--challenge-180/laurent-rosenfeld/kotlin/ch-1.kt16
-rw-r--r--challenge-180/laurent-rosenfeld/lua/ch-1.lua15
-rw-r--r--challenge-180/laurent-rosenfeld/python/ch-1.py9
-rw-r--r--challenge-180/laurent-rosenfeld/ring/ch-1.ring12
-rw-r--r--challenge-180/laurent-rosenfeld/ruby/ch-1.rb14
-rw-r--r--challenge-180/laurent-rosenfeld/scala/ch-1.scala26
8 files changed, 126 insertions, 0 deletions
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<Char,Int>() // 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
+ }
+ }
+}