Skip to content

readded files#4

Open
Satbek wants to merge 1 commit intomasterfrom
branch_for_review
Open

readded files#4
Satbek wants to merge 1 commit intomasterfrom
branch_for_review

Conversation

@Satbek
Copy link
Copy Markdown
Owner

@Satbek Satbek commented Jul 3, 2018

No description provided.

Comment thread happy_number.pl
my %seen;
my $res = 1;
while () {
if (exists $seen{$num}) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а зачем if exists?
не лучше бы просто if ($seen) ?

Comment thread happy_number.pl
$num = sum map {$_**2} split //, $num;
p $num;
sleep(1);
last if ($num == 1);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

скобки лишние, я бы убрал

Comment thread happy_number.pl
$res = 0;
last;
}
$seen{$num}++;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут лучше ставить $seen{$num} = 1, так как ++ только путает читателя. Ведь нам не нужно же количество?

Comment thread pascal.pl
}
my @res;
push @res, $level;
for (1..$count - 1) {
Copy link
Copy Markdown
Collaborator

@nurzhan-saktaganov nurzhan-saktaganov Jul 3, 2018

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 -- ко всем

Comment thread pascal.pl
sub next_level {
my $arr = shift;
my @res;
my $first = 0;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

переменная first запутывает...

Comment thread pascal.pl
push @res, $i + $first;
$first = $i;
}
push @res, @{$arr}[-1];
Copy link
Copy Markdown
Collaborator

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;
}

Comment thread reverse.pl
my $i = 0;
my $j = $#string;
while ($i < $j) {
my $tmp = $string[$i];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 за in place reverse.

Comment thread pivot.pl
my $sum_left = 0;
my $sum_right = 0;
for (1..$#arr) {
$sum_right += $arr[$_];
Copy link
Copy Markdown
Collaborator

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]; 

Copy link
Copy Markdown
Collaborator

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?

Так как крайние элементы им быть не могут.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Эту задачу надо дополнительно обсудить, после твоего ответа.

Comment thread single_number.pl
my @arr2 = (2, 2);
my $res = 0;
my %hash;
@hash{@arr1} = ();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Эта строка лишняя

Comment thread sublicates.pl
my %hash;

my $res = 0;
for (@arr) {
Copy link
Copy Markdown
Collaborator

@nurzhan-saktaganov nurzhan-saktaganov Jul 3, 2018

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;
}

Comment thread sum2.pl


while ($l < $r) {
if ($numbers[$l] + $numbers[$r] == $target) {
Copy link
Copy Markdown
Collaborator

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";

Comment thread spiral.pl
#!/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.
Copy link
Copy Markdown
Collaborator

@nurzhan-saktaganov nurzhan-saktaganov Jul 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 Блин, такая задача была в 10 классе, забыл как делать. Но делается гораздо проще, напишу уже завтра.

Comment thread pivot.pl

sub return_pivod(@) {
my @arr = @_;
return -1 unless (@arr);
Copy link
Copy Markdown
Collaborator

@nurzhan-saktaganov nurzhan-saktaganov Jul 3, 2018

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;

Comment thread pivot.pl
for (1..$#arr) {
$sum_right += $arr[$_];
}
while ($sum_left != $sum_right) {
Copy link
Copy Markdown
Collaborator

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";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants