Skip to content

Conversation

@Satbek
Copy link
Owner

@Satbek Satbek commented Jul 3, 2018

No description provided.

my %seen;
my $res = 1;
while () {
if (exists $seen{$num}) {
Copy link
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) ?

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

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}++;
Copy link
Collaborator

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) {
Copy link
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 -- ко всем

sub next_level {
my $arr = shift;
my @res;
my $first = 0;
Copy link
Collaborator

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];
Copy link
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;
}

my $i = 0;
my $j = $#string;
while ($i < $j) {
my $tmp = $string[$i];
Copy link
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.

my $sum_left = 0;
my $sum_right = 0;
for (1..$#arr) {
$sum_right += $arr[$_];
Copy link
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
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
Collaborator

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} = ();
Copy link
Collaborator

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) {
Copy link
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;
}



while ($l < $r) {
if ($numbers[$l] + $numbers[$r] == $target) {
Copy link
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";

#!/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
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 классе, забыл как делать. Но делается гораздо проще, напишу уже завтра.


sub return_pivod(@) {
my @arr = @_;
return -1 unless (@arr);
Copy link
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;

for (1..$#arr) {
$sum_right += $arr[$_];
}
while ($sum_left != $sum_right) {
Copy link
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.

3 participants