↳
$users = [['name' => 'lucas', 'score' => 10], ['name' => 'pablo', 'score' => 5], ['name' => 'luis', 'score' => 7], ['name' => 'diego', 'score' => 10]]; $res = []; foreach($users as $user) { $res[$user['score']][] = $user['name']; } krsort($res); foreach($res as $score => $users) { if(count($users) > 1) natsort($users); foreach($users as $user) { echo "#".$user." "; } } echo "\n"; Meno
↳
Can be done easily, if we use a stable sort. I dont know perl; but I am guessing the provided data structure is a hashtable (or a dictionary!) sorted_names = sorted(users.keys()) this will sort all names in alphabetical order Now python's sort is stable; so we can simply finish this by: return sorted(sorted_names, key=lambda x: users[x], reverse=True) Meno
↳
'John', 'score' => 10], ['name' => 'Bob', 'score' => 1], ['name' => 'Carlos', 'score' => 5], ['name' => 'Alice', 'score' => 5], ['name' => 'Donald', 'score' => 7] ]; $list = []; foreach ($users as $user) { $name = $user['name']; $score = $user['score']; if (!isset($list[$score])) { $list[$score] = [$name]; continue; } //User with same count, push the name array_push($list[$score], $name); } krsort($list); foreach ($list as $users) { if (count($users) > 1) { natsort($users); } foreach($users as $user) { echo $user . "\n"; } } Meno
↳
my @A = ( 3,1,2,4,3 ); my @B = ( 1,4,4,5,5 ); my $hash = +{}; my @C; for ( @A ) { $hash->{$_} = 1; } for ( @B ) { $hash->{$_} = 2 if $hash->{$_}; } for ( keys %$hash ) { push @C, $_ if $hash->{$_} > 1; } Meno
↳
my @a=(1,2,3,4,8,9,0); my @b=(2,5,6,8); my %a = map{$_ => undef} @a; my @ix = grep { exists( $a{$_} ) } @b; Meno
↳
in the previous post was a small error .. correct answer is reposted here #!/usr/bin/perl my @a = ('3','1','2','4','44'); my @b = ('23','44','1','4'); print "@a \n"; print "@b \\n"; my @res; my %a_hash =(); foreach (@a) { ++$a_hash{$_}; }; foreach (@b) { if ($a_hash{$_} >0) {push @res, $_; }; }; print "Common values @res\n"; Meno
↳
I answered in Python. I gave them two answers as under def get_diff(a,b): c=[] for i in a: if not(i in b) and not(i in c): c.append(i) for i in b: if not(i in a) and not(i in c): c.append(i) return c or set.union(set(a),set(b))-set.intersection(set(a),set(b)) Meno
↳
In java: Integer[] input = new Integer[] {1, 7, 8, 2, 4, 5}; Integer[] input2 = new Integer[] {3, 5, 1, 7, 6, 9}; Set set = new HashSet(Arrays.asList(input)); Set set2 = new HashSet(Arrays.asList(input2)); Set intersection = new HashSet(set); intersection.retainAll(set2); set.removeAll(intersection); set2.removeAll(intersection); set.addAll(set2); Integer[] uniques = new Integer[set.size()]; set.toArray(uniques); Meno
↳
use strict; use warnings; use Data::Dumper qw(Dumper); my @a1 = (1, 7, 8, 2, 4, 5); my @a2 = (3, 5, 1, 7, 6, 9); my @r = (); for my $item (@a1) { if(!grep {$_ == $item} @a2) { push @r, $item; } } for my $item (@a2) { if(!grep {$_ ==$item} @a1) { push @r, $item; } } print Dumper \@r; Meno
↳
my @input = (1,2,3,1,10,4,3,4,5,2,3,4); my $seen = {} my @result = grep { not $seen->{$_} } @input; Meno
↳
hi
↳
aliissa
↳
#!/usr/bin/env perl use strict; use warnings; &main(); sub main { my %hist; open (my $fh, ") { my $line = lc($_); @arr = split("", $line); foreach my $ch (@arr) { $hist{$ch}++; $char_counter++; $most = $hist{$ch} if $most = $most-$a) { print "x "; } else { print " "; } } print "\n"; } print " a b c d e f g h i j k l m n o p q r s t u v w x y z\n"; } Meno
↳
#!/bin/perl # # # # my %tab; while() { chomp; map { ++$tab{$_} } split // ; } foreach my $key (sort keys %tab) { print '*' x $tab{$key}; print "$key\n"; } __DATA__ abactor abadite abaculus abacus Meno
↳
#!/bin/perl # # All three conditions a,b,c is here. # use strict; use warnings; open my $fh, 'input.txt' or die('Cannot open file.'); my %counter; my $max = 0; while () { map { $max = ++$counter{$_} > $max ? $counter{$_} : $max } grep { $_ =~ /[a-z]/ } split //, lc $_; } foreach my $k ( reverse sort { $counter{$a} $counter{$b} } keys %counter ) { print ( ('*' x int( 79 / $max * $counter{$k} ) ) . $k . ($counter{$k} == $max ? '' : "\n")); } print "\n"; close($fh); Meno
↳
The above answer will perform very poorly since you are using a Depth First Search. In the world wide web the depth of your graph can be practically infinite therefore you should have used BFS (breadth first search) Meno
↳
/** * Notes: * ------ * This solution assumes that there must be a sequence of hops from p1 to p2. * BFS uses a large amount of memory because we have to store the pointers. */ public static int getHopsFrom(int p1, int p2) { Queue queue = new LinkedList(); ArrayList pages = getPages(p1); HashSet visitedPages = new HashSet(); visitedPages.add(p1); queue.addAll(pages); int levelSize = pages.size(); int newLevelSize = 0; int hops = 1 ; while(!queue.isEmpty()) { int currentPage = queue.remove(); if(currentPage==p2) { return hops; } else { if(!visitedPages.contains(currentPage)) { queue.addAll(getPages(currentPage)); newLevelSize+= getPages(currentPage).size(); visitedPages.add(currentPage); } } levelSize--; if(levelSize == 0) { hops++; levelSize = newLevelSize ; newLevelSize = 0; } } return -1; } Meno
↳
public static int GetHops(int p1, int p2) { Queue currentLevel = new Queue(); Queue nextLevel = new Queue(); int hops = 0; HashSet visited = new HashSet(); currentLevel.Enqueue(p1); while (currentLevel.Count > 0) { int temp = currentLevel.Dequeue(); visited.Add(temp); if (temp == p2) { return hops; } List pages = GetPage(temp); foreach (int item in pages) { if (!visited.Contains(item)) { nextLevel.Enqueue(item); } } if (currentLevel.Count == 0) { currentLevel = new Queue(nextLevel); nextLevel.Clear(); hops++; } } return -1; } Meno
↳
With hash table is fastest
↳
sub anagram_part { my ( $str, $substr ) = @_; my @str = split //, $str; my @substr = split //, $substr; L: while ( @str && @substr ) { my $cl = shift @substr; while ( my $c = shift @str ) { goto L if $cl eq $c; } return 0; } return 0 if scalar @substr; return 1; } Meno
↳
public static boolean check(String original, String subString) { if(original == null || subString == null) { return false; } if("".equals(subString)) { return true; } int positionAtOriginal = 0; for(int i = 0; i < subString.length(); i++) { char currentChar = subString.charAt(i); boolean findCurrentChar = false; while(positionAtOriginal < original.length()) { if(original.charAt(positionAtOriginal) == currentChar) { findCurrentChar = true; } positionAtOriginal++; if(findCurrentChar) { break; } } if(!findCurrentChar) { return false; } } return true; } Meno
↳
my @array = (1,2,4,7,5,6,3,2); my @sub; my $i=0; while ($i<=$#array) { my @set; push(@set,$array[$i]); for(my $j=$i;$j<=$#array;$j++){ if($array[$j]<=$array[$j+1]){ push(@set,$array[$j+1]); } else{ $i= $j+1; last; } } push(@sub,\@set); } Meno
↳
$src = [1,2,4,7,5,6,3,2]; foreach (@{$src}) { ++$idx if $dest->[$idx]->[-1] > $_; push @{$dest->[$idx]}, $_; } # result: $dest = [[1,2,4,7][5,6],[3],[2]] Meno
↳
use strict; use warnings; use Data::Dumper qw(Dumper); my @main_list = (1, 2, 4, 7, 5, 6, 3, 2); my $prev = $main_list[0]; my @result = (); my $index = 0; foreach my $num (@main_list) { if($prev > $num) { print Dumper \@result; @result = (); $index++; } push @result, $num; $prev = $num; } print Dumper \@result; Meno
↳
#*************************************************************************** # Creator: Zoran Hristov 03-02-2015 # # NAME: # convertDecToHex # # # PARAMETERS: # $decimal - decimal input # # RETURNS: # $rez - hexadecimal representation of the decimal input #*************************************************************************** sub convertDecToHex { my $decimal = $_[0]; my %conversionHash; $conversionHash{0} = '0'; $conversionHash{1} = '1'; $conversionHash{2} = '2'; $conversionHash{3} = '3'; $conversionHash{4} = '4'; $conversionHash{5} = '5'; $conversionHash{6} = '6'; $conversionHash{7} = '7'; $conversionHash{8} = '8'; $conversionHash{9} = '9'; $conversionHash{10} = 'A'; $conversionHash{11} = 'B'; $conversionHash{12} = 'C'; $conversionHash{13} = 'D'; $conversionHash{14} = 'E'; $conversionHash{15} = 'F'; my $decimali = $decimal; my $rez; my $rem = 1; while ( $decimal ne 0 ) { print " curent print reminder decimal \n"; $rem = $decimal % 16; print "curent devision returns $rez\n"; my $ind = $decimal - $rem; if ( $ind = 0 ) { $rez = $conversionHash{$rem} ; } else { print "For curent decimal $decimal we will transalte to $conversionHash{$rem}\n"; $decimal = ( $decimal - $rem ) / 16 ; $rez = $conversionHash{$rem} . $rez; } } print "\n Input decimal value $decimali was translated in hex reresented by: $rez \n"; } # end of sub convertDecToHex Meno
↳
my @digits = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F); my $num = 312312; { my $out = ""; use integer; while($num > 16) { $m = $num % 16; $num = $num / 16; $out = $digits[$m] . $out; } print "$num$out\n"; } Meno
↳
That's more perlish, universal approach :X perl -e '($n,$b)=@ARGV;@m=(0..9,"A".."F");$o="";while($n>0){$r=$n%$b;$n=int$n/$b;$o="$m[$r]$o"};print $o' 256 16 Meno
↳
I answered as compare every element of the first one if exist in the second one and vice versa Meno
↳
sub difference { my ($arr1, $arr2) = @_; %count = (); foreach $element (@{$arr1}, @{$arr2}) { $count->{$element}++; } @difference = (); foreach $key (keys $count) { if ($count->{$key} eq 1) { push (@difference, $key); } } print "Uncommon numbers from both arrays are : @difference", "\n"; } Meno
↳
my $arr1 = [1,3,5,6,7,8,9,77]; my $arr2 = [43,6,3,5,234,66,4]; print join ' ', @{difference($arr1, $arr2)}; sub difference{ my ($arr1, $arr2) = @_; my @difference = (); my $count = {}; $count->{$_}++ for (@$arr1, @$arr2); for (keys %$count) { push @difference, $_ if ($count->{$_} == 1) ; } return \@difference; } Meno