-
Notifications
You must be signed in to change notification settings - Fork 0
readded files #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| my %seen; | ||
| my $res = 1; | ||
| while () { | ||
| if (exists $seen{$num}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а зачем if exists?
не лучше бы просто if ($seen) ?
| $num = sum map {$_**2} split //, $num; | ||
| p $num; | ||
| sleep(1); | ||
| last if ($num == 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
скобки лишние, я бы убрал
| $res = 0; | ||
| last; | ||
| } | ||
| $seen{$num}++; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тут лучше ставить $seen{$num} = 1, так как ++ только путает читателя. Ведь нам не нужно же количество?
| } | ||
| my @res; | ||
| push @res, $level; | ||
| for (1..$count - 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше явно вынести все в функции имхо: main, next_level, get_first_n_rows
про вынос в main -- ко всем
| sub next_level { | ||
| my $arr = shift; | ||
| my @res; | ||
| my $first = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
переменная first запутывает...
| push @res, $i + $first; | ||
| $first = $i; | ||
| } | ||
| push @res, @{$arr}[-1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше написать тупым и очевидным образом. Ведь код в первую очередь для человека:
sub next_level {
my ($level) = @_;
my @next_level;
push @next_level, 1;
for (my $i = 0; $i < @$level - 1; ++$i) {
push @next_level, $level->[$i] + $level->[$i + 1];
}
push @next_level, 1;
return \@next_level;
}
| my $i = 0; | ||
| my $j = $#string; | ||
| while ($i < $j) { | ||
| my $tmp = $string[$i]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 за in place reverse.
| my $sum_left = 0; | ||
| my $sum_right = 0; | ||
| for (1..$#arr) { | ||
| $sum_right += $arr[$_]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe? :)
use List::Util qw(sum);
...
my $sum_right = sum(@arr) - $arr[0];
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Хотяяя, пивотом могут быть индексы 1 <= pivot <= len(arr) - 2?
Так как крайние элементы им быть не могут.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Эту задачу надо дополнительно обсудить, после твоего ответа.
| my @arr2 = (2, 2); | ||
| my $res = 0; | ||
| my %hash; | ||
| @hash{@arr1} = (); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Эта строка лишняя
| my %hash; | ||
|
|
||
| my $res = 0; | ||
| for (@arr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Оформил бы в функцию лучше. Было бы проще. И можно было бы заюзать ранний выход.
sub has_duplicates {
my (@arr) = @_;
my %seen;
for (@arr) {
return 1 if $seen{$_};
$seen{$_}++;
}
return;
}
|
|
||
|
|
||
| while ($l < $r) { | ||
| if ($numbers[$l] + $numbers[$r] == $target) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно компактнее
while ($l < $r) {
my $sum = $numbers[$l] + $numbers[$r];
last if $sum == $target;
$sum < $target ? $l++ : $r--;
}
print "$l $r\n";
| #!/usr/bin/perl | ||
| use 5.016; | ||
| use DDP; | ||
| # Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Блин, такая задача была в 10 классе, забыл как делать. Но делается гораздо проще, напишу уже завтра.
|
|
||
| sub return_pivod(@) { | ||
| my @arr = @_; | ||
| return -1 unless (@arr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Как минимум должно быть 3 элемента.
return -1 if @arr < 3;
| for (1..$#arr) { | ||
| $sum_right += $arr[$_]; | ||
| } | ||
| while ($sum_left != $sum_right) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я бы в коде сделал акцент на том, что перебирается каждая позиция пивота.
И для каждой позиции проверятся равность суммы.
use strict;
use warnings;
use List::Util qw(sum);
sub find_pivot {
my @a = @_;
return -1 if @a < 3;
my $sum_l = $a[0];
my $sum_r = sum(@a) - sum(@a[0,1]);
for my $i (1..$#a - 1) {
return $i if $sum_l == $sum_r;
$sum_l += $a[$i];
$sum_r -= $a[$i + 1];
}
return -1;
}
my @a = (1, 7, 3, 6, 5, 6);
print find_pivot(@a) . "\n";
No description provided.