aboutsummaryrefslogtreecommitdiff
path: root/challenge-071
diff options
context:
space:
mode:
authorWalt Mankowski <waltman@pobox.com>2020-07-30 20:39:24 -0400
committerWalt Mankowski <waltman@pobox.com>2020-07-30 20:39:24 -0400
commitdb3b23df2d1a6b862efa30d09ca3d243333d5620 (patch)
tree11f4fbdf65f8ba93271d2691082fe42f1502a7fc /challenge-071
parent4d163f40a1838229d39ae629aed639f946cab096 (diff)
downloadperlweeklychallenge-club-db3b23df2d1a6b862efa30d09ca3d243333d5620.tar.gz
perlweeklychallenge-club-db3b23df2d1a6b862efa30d09ca3d243333d5620.tar.bz2
perlweeklychallenge-club-db3b23df2d1a6b862efa30d09ca3d243333d5620.zip
for task 1, make the elements on the array unique
Diffstat (limited to 'challenge-071')
-rw-r--r--challenge-071/walt-mankowski/c/ch-1.c20
-rw-r--r--challenge-071/walt-mankowski/perl/ch-1.pl8
-rw-r--r--challenge-071/walt-mankowski/python/ch-1.py4
3 files changed, 22 insertions, 10 deletions
diff --git a/challenge-071/walt-mankowski/c/ch-1.c b/challenge-071/walt-mankowski/c/ch-1.c
index 5e795a604c..830f14f7e9 100644
--- a/challenge-071/walt-mankowski/c/ch-1.c
+++ b/challenge-071/walt-mankowski/c/ch-1.c
@@ -1,12 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
+#include <string.h>
+
+void fisher_yates(int *a, const int N) {
+ for (int i = N-1; i >= 1; i--) {
+ const int j = rand() % (i+1);
+ const int tmp = a[i];
+ a[i] = a[j];
+ a[j] = tmp;
+ }
+}
const int *make_array(const int N) {
- int *a = calloc(N+2, sizeof(int));
+ int digits[50];
+ for (int i = 0; i < 50; i++)
+ digits[i] = i+1;
- for (int i = 1; i <= N; i++)
- a[i] = 1 + rand() % 50;
+ fisher_yates(digits, 50);
+
+ int *a = calloc(N+2, sizeof(int));
+ memcpy(a+1, digits, N * sizeof(int));
return a;
}
diff --git a/challenge-071/walt-mankowski/perl/ch-1.pl b/challenge-071/walt-mankowski/perl/ch-1.pl
index aae859edaa..73d51adb62 100644
--- a/challenge-071/walt-mankowski/perl/ch-1.pl
+++ b/challenge-071/walt-mankowski/perl/ch-1.pl
@@ -3,6 +3,7 @@ use strict;
use warnings;
use feature qw(:5.32);
use experimental qw(signatures);
+use List::Util qw(shuffle);
# TASK #1 › Peak Element
# Submitted by: Mohammad S Anwar
@@ -34,11 +35,8 @@ say "Array: [@a[1..$#a-1]]";
say "Peak: [@p]";
sub make_array($N) {
- my @a = (0);
- push @a, map { int(rand(50)) + 1} 1..$N;
- push @a, 0;
-
- return @a;
+ my @a = shuffle 1..50;
+ return (0, @a[0..$N-1], 0);
}
sub peaks(@a) {
diff --git a/challenge-071/walt-mankowski/python/ch-1.py b/challenge-071/walt-mankowski/python/ch-1.py
index e91eacb55e..2cb2ad2260 100644
--- a/challenge-071/walt-mankowski/python/ch-1.py
+++ b/challenge-071/walt-mankowski/python/ch-1.py
@@ -1,8 +1,8 @@
from sys import argv
-from random import randint
+from random import sample
def make_array(N):
- return [0] + [randint(1,50) for _ in range(N)] + [0]
+ return [0] + sample(range(1,51), k=50)[0:N] + [0]
def peaks(a):
p = []