Skip to content

Instantly share code, notes, and snippets.

@adamcrussell
Last active October 5, 2020 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrussell/e18f081d711be5a49aea6e6665463d73 to your computer and use it in GitHub Desktop.
Save adamcrussell/e18f081d711be5a49aea6e6665463d73 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 080
#include <iostream>
#include <algorithm>
#include <vector>
/*
* You are given an unsorted list of integers @N.
* Write a script to find out the smallest positive number missing.
*/
int least_missing(int n[]){
std::sort (n, n + 4);
std::vector<int> numbers (n, n + 4);
for(int i = n[0]; i < n[3]; i++){
std::vector<int>::iterator itr = std::find(numbers.begin(), numbers.end(), i);
if (itr == std::end(numbers)) {
if(i > 0)
return i;
}
}
return -1;
}
int main(int argc, char** argv){
int N[4] = {5, 2, -2, 0};
int i = least_missing(N);
std::cout << "the least positive missing number is " << i << std::endl;
}
/*
* You are given an unsorted list of integers N.
* Write a script to find out the smallest positive number missing.
*/
n([2, 0, -1]).
missing_smallest([H|T], X):-
H >= 0,
last(T, Last),
between(H, Last, X),
\+ member(X, [H|T]).
missing_smallest([_|T], X):-
missing_smallest(T, X).
main:-
n(N),
sort(N, Sorted),
missing_smallest(Sorted, Missing),
writeln(Missing),
halt.
use strict;
use warnings;
##
# You are given an unsorted list of integers @N.
# Write a script to find out the smallest positive number missing.
##
sub least_missing{
my(@numbers) = @_;
@numbers = sort @numbers;
for my $i ($numbers[0] .. $numbers[@numbers - 1]){
my @a = grep { $_ == $i } @numbers;
return $i if(!@a && $i > 0);
}
return undef;
}
MAIN:{
my @N;
@N = (5, 2, -2, 0);
my $least_missing = least_missing(@N);
print "The least mising number from (" .
join(",", @N) . ") is $least_missing\n";
@N = (1, 8, -1);
$least_missing = least_missing(@N);
print "The least mising number from (" .
join(",", @N) . ") is $least_missing\n";
@N = (2, 0, -1);
$least_missing = least_missing(@N);
print "The least mising number from (" .
join(",", @N) . ") is $least_missing\n";
}
#include <iostream>
/*
* You are given rankings of @N candidates.
* Write a script to find out the total candies needed for all candidates.
* You are asked to follow the rules below:
* a) You must given at least one candy to each candidate.
* b) Candidate with higher ranking get more candies than their immediate
* neighbors on either side.
*/
int count_candies(int candidates[]){
int candies = 4;
for(int i = 0; i < 3; i++){
if((i - 1) >= 0){
if(candidates[i] > candidates[i - 1]){
candies++;
}
}
if((i + 1) < 4){
if(candidates[i] > candidates[i + 1]){
candies++;
}
}
}
return candies;
}
int main(int argc, char** argv){
int N[4] = {1, 4, 3, 2};
int i = count_candies(N);
std::cout << "the number of candies is " << i << std::endl;
}
/*
* You are given rankings of N candidates.
* Write a script to find out the total candies needed for all candidates.
* You are asked to follow the rules below:
* a) You must given at least one candy to each candidate.
* b) Candidate with higher ranking get more candies than their immediate
* neighbors on either side.
*/
n([1, 2, 2]).
%n([1, 4, 3, 2]).
candies(N, Candies):-
length(N, CandiesAccum),
candies(N, CandiesAccum, Candies).
candies([], Candies, Candies).
candies([H0, H1|[]], CandiesAccum, Candies):-
H0 > H1,
C is CandiesAccum + 1,
candies([], C, Candies).
candies([_, _|[]], CandiesAccum, Candies):-
candies([], CandiesAccum, Candies).
candies([H0, H1|T], CandiesAccum, Candies):-
H0 > H1,
C is CandiesAccum + 1,
candies([H1|T], C, Candies).
candies([H0, H1|T], CandiesAccum, Candies):-
H1 > H0,
C is CandiesAccum + 1,
candies([H1|T], C, Candies).
main:-
n(N),
candies(N, C),
writeln(C),
halt.
use strict;
use warnings;
##
# You are given rankings of @N candidates.
# Write a script to find out the total candies needed for all candidates.
# You are asked to follow the rules below:
# a) You must given at least one candy to each candidate.
# b) Candidate with higher ranking get more candies than their immediate
# neighbors on either side.
##
sub count_candies{
my(@candidates) = @_;
my $candies = @candidates;
for my $i (0 .. (@candidates - 1)){
if($candidates[$i - 1]){
$candies++ if $candidates[$i] > $candidates[$i - 1];
}
if($candidates[$i + 1]){
$candies++ if $candidates[$i] > $candidates[$i + 1];
}
}
return $candies;
}
MAIN:{
my @N;
my $number_candies;
@N = (1, 2, 2);
$number_candies = count_candies(@N);
print "The number of candies for (" .
join(",", @N) . ") is $number_candies\n";
@N = (1, 4, 3, 2);
$number_candies = count_candies(@N);
print "The number of candies for (" .
join(",", @N) . ") is $number_candies\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment