Skip to content

Instantly share code, notes, and snippets.

@sugitach
Last active August 29, 2015 14:22
Show Gist options
  • Save sugitach/3c803983154f14a5efdc to your computer and use it in GitHub Desktop.
Save sugitach/3c803983154f14a5efdc to your computer and use it in GitHub Desktop.
1時間以内に解けなければプログラマ失格となってしまう5つの問題
let rec _sum lst ret = match lst with
[] -> ret
| first :: rest -> _sum rest (ret + first)
let sum lst = _sum lst 0
let x = sum [1;2;3]
let x = sum [10;2;31]
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use feature 'say';
my @list = (20,1,3,205,6,7,8,543,7,866,3,23,5);
sub a_for {
my @list = @_;
my $ret = 0;
for (@list) {
$ret += $_;
}
return $ret;
}
sub a_while {
my @list = @_;
my $ret = 0;
while (local $_ = shift @list) {
$ret += $_;
}
return $ret;
}
sub a_recursive {
my $odd = shift;
my $list = shift;
if (@$list) {
my $now = shift @$list;
return a_recursive($odd + $now, $list);
} else {
return $odd;
}
}
say " FOR : ", a_for(@list);
say " WHITE : ", a_while(@list);
my @l = @list;
say "RECURSIVE : ", a_recursive(0, \@l);
let lst1 = [2;4;6;8;10]
let lst2 = [1;3]
let rec _merge_list l1 l2 ret = match l1 with
[] -> (match l2 with
[] -> ret
| _::_ -> _merge_list l2 [] ret)
| first :: rest -> _merge_list l2 rest (first::ret)
let merge_list l1 l2 = List.rev (_merge_list l1 l2 [])
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use feature 'say';
my @list1 = (20,1,3,205,6,7,8,543,7,866,3,23,5);
my @list2 = (2,3,53,23,677,6,44,3,21,6,78,2);
sub odd_list {
my $list1 = shift;
my $list2 = shift;
my @ret = ();
my $c = $#$list1 > $#$list2 ? $#$list1 : $#$list2;
for (0..$c) {
push @ret, shift @$list1 if (@$list1);
push @ret, shift @$list2 if (@$list2);
}
return @ret;
}
say join ',', odd_list(\@list1, \@list2);
let new_fibo lst = match lst with
[] -> Big_int.big_int_of_int 0
| first::rest -> match rest with
[] -> Big_int.big_int_of_int 1
| second :: _ -> Big_int.add_big_int first second
let rec _fibo cnt lst = match cnt with
0 -> lst
| _ -> _fibo (cnt - 1) ((new_fibo lst)::lst)
let fibo cnt = List.rev(_fibo cnt [])
let x = List.map Big_int.string_of_big_int (fibo 100)
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use feature 'say';
use bigint;
my @answer = (0, 1);
sub fibonacci {
my @src = @_;
return $src[-1]+$src[-2];
}
while (@answer <= 100) {
push @answer, fibonacci(@answer);
}
my $cnt = 0;
for (@answer) {
say sprintf ('%3d : ', $cnt++), $_;
}
let src = [1;2;3;4;5;16]
let cmp a b = let x = a ^ b in
let y = b ^ a in
if x == y then 0
else if x < y then 1
else -1
let getMax lst = String.concat "" (List.sort cmp (List.map string_of_int lst))
let test = int_of_string (getMax src)
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use feature 'say';
use bigint;
my @list = (9,91,919);
my @ans;
@ans = getMax(@list);
say join '', @ans;
sub getMax {
return sort { $b.$a <=> $a.$b } @_;
}
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use feature 'say';
use bigint;
my @num = (1..9);
for my $i (0..3**9) {
my @ex;
for my $c (0..7) {
my $ex = $i % 3;
push @ex, ($ex == 0 ? '' : $ex == 1 ? '+' : '-');
$i /= 3;
}
my $ans = join '', odd_list(\@num, \@ex);
my $val = eval $ans;
if ($val == 100) {
say $ans;
}
}
sub odd_list {
my $list1 = shift;
my $list2 = shift;
my @ret = ();
my @list1 = @$list1;
my @list2 = @$list2;
my $c = $#list1 > $#list2 ? $#list1 : $#list2;
for (0..$c) {
push @ret, shift @list1 if (@list1);
push @ret, shift @list2 if (@list2);
}
return @ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment