aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-10 17:00:23 +0100
committerGitHub <noreply@github.com>2024-09-10 17:00:23 +0100
commit0405f5ff69aefd51a019fdde39cdb1f75ecedeae (patch)
tree709b26f7c3556dd0ea37b1a20c018ec6e979ab2b
parent07a8cae3a7d33c702ac239d3f1065855e6e8578b (diff)
parent9c5286b32ea8e4cc8affd0f3ece4429ba0ab488d (diff)
downloadperlweeklychallenge-club-0405f5ff69aefd51a019fdde39cdb1f75ecedeae.tar.gz
perlweeklychallenge-club-0405f5ff69aefd51a019fdde39cdb1f75ecedeae.tar.bz2
perlweeklychallenge-club-0405f5ff69aefd51a019fdde39cdb1f75ecedeae.zip
Merge pull request #10817 from zapwai/branch-for-286
Branch for 286
-rw-r--r--challenge-286/zapwai/blog.txt1
-rw-r--r--challenge-286/zapwai/c/ch-1.c27
-rw-r--r--challenge-286/zapwai/c/ch-2.c38
-rw-r--r--challenge-286/zapwai/javascript/286-1.html31
-rw-r--r--challenge-286/zapwai/javascript/ch-2.js22
-rw-r--r--challenge-286/zapwai/perl/ch-1.pl7
-rw-r--r--challenge-286/zapwai/perl/ch-2.pl24
-rw-r--r--challenge-286/zapwai/python/ch-1.py4
-rw-r--r--challenge-286/zapwai/python/ch-2.py18
-rw-r--r--challenge-286/zapwai/r/ch-1.r8
-rw-r--r--challenge-286/zapwai/r/ch-2.r21
-rw-r--r--challenge-286/zapwai/rust/ch-1.rs13
-rw-r--r--challenge-286/zapwai/rust/ch-2.rs26
13 files changed, 240 insertions, 0 deletions
diff --git a/challenge-286/zapwai/blog.txt b/challenge-286/zapwai/blog.txt
new file mode 100644
index 0000000000..32a85c73b1
--- /dev/null
+++ b/challenge-286/zapwai/blog.txt
@@ -0,0 +1 @@
+https://dev.to/zapwai/not-quite-a-quine-fon
diff --git a/challenge-286/zapwai/c/ch-1.c b/challenge-286/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..843211862b
--- /dev/null
+++ b/challenge-286/zapwai/c/ch-1.c
@@ -0,0 +1,27 @@
+#include <time.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+int main() {
+ FILE *fp;
+ fp = fopen("ch-1.c", "r");
+ if (fp == NULL) {
+ printf("cannot open file.");
+ exit(1);
+ }
+ char words[100][100];
+ int wordslen = 0;
+ char line[100];
+ while (fgets(line, sizeof(line), fp)) {
+ char *p = strtok(line, " ");
+ while(p != NULL) {
+ strcpy(words[wordslen++], p);
+ p = strtok(NULL, " ");
+ }
+ }
+ fclose(fp);
+ srand(time(NULL));
+ int num = rand() % wordslen;
+ char *word = words[num];
+ printf("%s\n", word);
+}
diff --git a/challenge-286/zapwai/c/ch-2.c b/challenge-286/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..48f7fb7b2b
--- /dev/null
+++ b/challenge-286/zapwai/c/ch-2.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#define MAXLEN 64
+int min(int a, int b) {return (a < b) ? a : b;}
+int max(int a, int b) {return (a < b) ? b : a;}
+void proc(int ints[], int intslen) {
+ printf("Input: ints = { ");
+ for (int i = 0; i < intslen; i++)
+ printf("%d ", ints[i]);
+ printf("}\n");
+ while (intslen > 2) {
+ int L[MAXLEN] = {};
+ int j = 0;
+ for (int i = 0; i < intslen/2; i++) {
+ if (i % 2 == 0) {
+ L[j] = min(ints[2*i], ints[2*i + 1]);
+ } else {
+ L[j] = max(ints[2*i], ints[2*i + 1]);
+ }
+ j++;
+ }
+ for (int i = 0; i < intslen/2; i++) {
+ ints[i] = L[i];
+ }
+ intslen /= 2;
+ }
+ printf("Output: %d\n", min(ints[0], ints[1]));
+}
+
+int main() {
+ int ints[] = {2, 1, 4, 5, 6, 3, 0, 2};
+ proc(ints, sizeof(ints)/sizeof(int));
+ int ints2[] = {0, 5, 3, 2};
+ proc(ints2, sizeof(ints2)/sizeof(int));
+ int ints3[] = {9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8};
+ proc(ints3, sizeof(ints3)/sizeof(int));
+}
+
+
diff --git a/challenge-286/zapwai/javascript/286-1.html b/challenge-286/zapwai/javascript/286-1.html
new file mode 100644
index 0000000000..0dde34add5
--- /dev/null
+++ b/challenge-286/zapwai/javascript/286-1.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>Upload Text File</title>
+</head>
+<body>
+ <h1>Upload a Text File</h1>
+ <input type="file" id="fileInput" name="file" accept=".html">
+ <script>
+ function handleFileUpload(event) {
+ const file = event.target.files[0];
+ if (!file) {
+ console.error('No file selected');
+ return;
+ }
+ const reader = new FileReader();
+ reader.onload = function(e) {
+ const content = e.target.result;
+ const words = content.split(/\s+/);
+ let num = Math.floor(Math.random()*words.length)
+ console.log(words[num]);
+ };
+ reader.readAsText(file);
+ }
+ const fileInput = document.getElementById('fileInput');
+ fileInput.addEventListener('change', handleFileUpload);
+ </script>
+</body>
+</html>
diff --git a/challenge-286/zapwai/javascript/ch-2.js b/challenge-286/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..b505804e8a
--- /dev/null
+++ b/challenge-286/zapwai/javascript/ch-2.js
@@ -0,0 +1,22 @@
+let ints = [2, 1, 4, 5, 6, 3, 0, 2];
+proc(ints);
+ints = [0, 5, 3, 2];
+proc(ints);
+ints = [9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8];
+proc(ints);
+
+function proc(ints) {
+ console.log("Input: ints =", ints);
+ while (ints.length > 2) {
+ let L = [];
+ for (let i = 0; i < ints.length/2; i++) {
+ if (i % 2 == 0) {
+ L.push(Math.min(ints[2*i], ints[2*i + 1]));
+ } else {
+ L.push(Math.max(ints[2*i], ints[2*i + 1]));
+ }
+ }
+ ints = L;
+ }
+ console.log("Output:", Math.min(ints[0], ints[1]));
+}
diff --git a/challenge-286/zapwai/perl/ch-1.pl b/challenge-286/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..6855004a80
--- /dev/null
+++ b/challenge-286/zapwai/perl/ch-1.pl
@@ -0,0 +1,7 @@
+open my $fh, "<$0";
+my @words;
+while (<$fh>) {
+ push @words, split " ", $_;
+}
+close $fh;
+print $words[int rand(@words)]."\n";
diff --git a/challenge-286/zapwai/perl/ch-2.pl b/challenge-286/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..c06401d753
--- /dev/null
+++ b/challenge-286/zapwai/perl/ch-2.pl
@@ -0,0 +1,24 @@
+use v5.38;
+use List::Util qw( min max );
+my @ints = (2, 1, 4, 5, 6, 3, 0, 2);
+proc(@ints);
+@ints = (0, 5, 3, 2);
+proc(@ints);
+@ints = (9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8);
+proc(@ints);
+
+sub proc(@ints) {
+ say "Input: \@ints = (".join(", ", @ints).")";
+ while (scalar @ints > 2) {
+ my @L;
+ for my $i (0 .. ($#ints - 1)/2) {
+ if ($i % 2 == 0) {
+ push @L, min($ints[2*$i], $ints[2*$i + 1]);
+ } else {
+ push @L, max($ints[2*$i], $ints[2*$i + 1]);
+ }
+ }
+ @ints = @L;
+ }
+ say "Output: " . min(@ints);
+}
diff --git a/challenge-286/zapwai/python/ch-1.py b/challenge-286/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..546468060a
--- /dev/null
+++ b/challenge-286/zapwai/python/ch-1.py
@@ -0,0 +1,4 @@
+import random
+fil=open("ch-1.py","r")
+words=fil.read().split()
+print(words[random.randint(0,len(words)-1)])
diff --git a/challenge-286/zapwai/python/ch-2.py b/challenge-286/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..88cd9be573
--- /dev/null
+++ b/challenge-286/zapwai/python/ch-2.py
@@ -0,0 +1,18 @@
+def proc(ints) :
+ print("Input: ints =", ints);
+ while (len(ints) > 2):
+ L = []
+ for i in range(int(len(ints)/2)):
+ if i % 2 == 0 :
+ L.append(min(ints[2*i], ints[2*i + 1]))
+ else :
+ L.append(max(ints[2*i], ints[2*i + 1]))
+ ints = L
+ print("Output:", min(ints))
+
+ints = [2, 1, 4, 5, 6, 3, 0, 2]
+proc(ints)
+ints = [0, 5, 3, 2]
+proc(ints)
+ints = [9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8]
+proc(ints)
diff --git a/challenge-286/zapwai/r/ch-1.r b/challenge-286/zapwai/r/ch-1.r
new file mode 100644
index 0000000000..5d1bbf21d0
--- /dev/null
+++ b/challenge-286/zapwai/r/ch-1.r
@@ -0,0 +1,8 @@
+lines <- readLines("ch-1.r")
+words <- c()
+for (l in lines) {
+ word = strsplit(l, " ")[[1]]
+ words <- append(words, word)
+}
+num <- sample(1:length(words), 1)
+cat(words[num],"\n")
diff --git a/challenge-286/zapwai/r/ch-2.r b/challenge-286/zapwai/r/ch-2.r
new file mode 100644
index 0000000000..c11e1d45b6
--- /dev/null
+++ b/challenge-286/zapwai/r/ch-2.r
@@ -0,0 +1,21 @@
+proc <-function(ints) {
+ cat("Input: ints =", ints, "\n")
+ while (length(ints) > 2) {
+ L <- c()
+ for (i in 1:as.integer((length(ints))/2)) {
+ if (i %% 2 != 0) {
+ L <- append(L, min(ints[2*i - 1], ints[2*i]))
+ } else {
+ L <- append(L, max(ints[2*i-1], ints[2*i]))
+ }
+ }
+ ints <- L
+ }
+ cat("Output:", min(ints), "\n")
+}
+ints <- c(2, 1, 4, 5, 6, 3, 0, 2)
+proc(ints)
+ints <- c(0, 5, 3, 2)
+proc(ints)
+ints <- c(9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8)
+proc(ints)
diff --git a/challenge-286/zapwai/rust/ch-1.rs b/challenge-286/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..c00d3ff5f4
--- /dev/null
+++ b/challenge-286/zapwai/rust/ch-1.rs
@@ -0,0 +1,13 @@
+use std::fs::File;
+use std::io::{self, Read};
+use rand::Rng;
+fn main() -> io::Result<()> {
+ let mut file = File::open("ch-1.rs")?;
+ let mut contents = String::new();
+ file.read_to_string(&mut contents)?;
+ let words : Vec<&str> = contents.split_whitespace().collect();
+ let mut rng = rand::thread_rng();
+ let num = rng.gen_range(0..words.len());
+ println!("{}", words[num]);
+ Ok(())
+}
diff --git a/challenge-286/zapwai/rust/ch-2.rs b/challenge-286/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..d528b1726f
--- /dev/null
+++ b/challenge-286/zapwai/rust/ch-2.rs
@@ -0,0 +1,26 @@
+use std::cmp::{max, min};
+
+fn main() {
+ let mut ints = vec![2, 1, 4, 5, 6, 3, 0, 2];
+ proc(ints);
+ let ints2 = vec![0, 5, 3, 2];
+ proc(ints2);
+ let ints3 = vec![9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8];
+ proc(ints3);
+}
+
+fn proc(mut ints : Vec<i32>) {
+ println!("Input: ints = {:?}", ints);
+ while ints.len() > 2 {
+ let mut l : Vec<i32> = Vec::new();
+ for i in 0 .. ints.len()/2 {
+ if i % 2 == 0 {
+ l.push(min(ints[2*i], ints[2*i + 1]));
+ } else {
+ l.push(max(ints[2*i], ints[2*i + 1]));
+ }
+ }
+ ints = l;
+ }
+ println!("Output: {}", ints.iter().min().unwrap());
+}