From 9571cceb51b187d9a9a5cb12cfe90152d74af0d1 Mon Sep 17 00:00:00 2001 From: Nurzhan Saktaganov Date: Tue, 3 Jul 2018 22:30:17 +0300 Subject: [PATCH] readded files --- diag_trav.pl | 73 ++++++++++++++++ diag_trav.py | 44 ++++++++++ file.txt | 0 happy_number.pl | 22 +++++ hash/minimum_index_sum_of_two_lists.pl | 73 ++++++++++++++++ partition.py | 16 ++++ pascal.pl | 44 ++++++++++ pivot.pl | 52 +++++++++++ pivot.py | 26 ++++++ reverse.pl | 23 +++++ single_number.pl | 10 +++ spiral.pl | 116 +++++++++++++++++++++++++ sublicates.pl | 17 ++++ sum2.pl | 36 ++++++++ sum2.py | 29 +++++++ 15 files changed, 581 insertions(+) create mode 100755 diag_trav.pl create mode 100755 diag_trav.py create mode 100644 file.txt create mode 100755 happy_number.pl create mode 100755 hash/minimum_index_sum_of_two_lists.pl create mode 100755 partition.py create mode 100755 pascal.pl create mode 100755 pivot.pl create mode 100755 pivot.py create mode 100755 reverse.pl create mode 100755 single_number.pl create mode 100755 spiral.pl create mode 100755 sublicates.pl create mode 100755 sum2.pl create mode 100755 sum2.py diff --git a/diag_trav.pl b/diag_trav.pl new file mode 100755 index 0000000..f190433 --- /dev/null +++ b/diag_trav.pl @@ -0,0 +1,73 @@ +#!/usr/bin/perl +use 5.016; +use DDP; +use List::Util qw /min max/; +# Given a matrix of M x N elements (M rows, N columns), return all elements +#of the matrix in diagonal order as shown in the below image. + +# Example: + +# Input: +# [ +# [ 1, 2, 3 ], +# [ 4, 5, 6 ], +# [ 7, 8, 9 ] +# ] +# Output: [1,2,4,7,5,3,6,8,9] +#my @matrix = ([1,2,3,4],[5,6,7,8],[9,10,11,12]); +my @matrix = ([1,2,3],[4,5,6]); +# 1 2 3 4 +# 5 6 7 8 +# 9 10 11 12 + +# 13 14 15 16 +my $A = $#matrix; +my $B = scalar(@{$matrix[0]}) - 1; +sub diag_down { + my $matrix = shift; + my ($i, $j) = @_; + my @res; + push @res, $matrix->[$i][$j]; + + while ($i != $#matrix and $j != 0) { + $i++; + $j--; + push @res, $matrix->[$i][$j]; + } + return \@res; +} + +my @res; +my $ans; +my $flag = 1; +for my $i (0..$B) { + $ans = diag_down(\@matrix, 0, $i); + if ($flag) { + $ans = [reverse @{$ans}]; + $flag = 0; + } else { + $flag = 1; + } + push @res, @{$ans}; +} + +for my $i (1..$A) { + $ans = diag_down(\@matrix, $i, $B); + if ($flag) { + $ans = [reverse @{$ans}]; + $flag = 0; + } else { + $flag = 1; + } + push @res, @{$ans}; +} +# my $ans = diag_down(\@matrix, 0,1); +# p $ans; + +p @res; +# 0,0 up +# 0,1 down +# 0,2 + +# 1,2 +# 2,2 diff --git a/diag_trav.py b/diag_trav.py new file mode 100755 index 0000000..2aa23de --- /dev/null +++ b/diag_trav.py @@ -0,0 +1,44 @@ +#!/usr/bin/python +class Solution: + def diag_down(self, i, j): + res = [] + res.append(self.matrix[i][j]) + while (i != self.A - 1 and j != 0): + i = i + 1 + j = j - 1 + res.append(self.matrix[i][j]) + return res + + def findDiagonalOrder(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + self.matrix = matrix + if (len(matrix) == 0): + return matrix + self.A = len(matrix) + self.B = len(matrix[0]) + res = [] + flag = True + for i in range(self.B): + ans = self.diag_down(0, i); + if (flag): + ans = ans[::-1] + flag = False + else: + flag = True + res += ans + for i in range(1,self.A): + ans = self.diag_down(i,self.B - 1) + if (flag): + ans = ans[::-1] + flag = False + else: + flag = True + res += ans + return res + +s = Solution() + +print(s.findDiagonalOrder([[2,3]])) \ No newline at end of file diff --git a/file.txt b/file.txt new file mode 100644 index 0000000..e69de29 diff --git a/happy_number.pl b/happy_number.pl new file mode 100755 index 0000000..185c07b --- /dev/null +++ b/happy_number.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use 5.016; +use DDP; +use List::Util qw/sum/; +my $num = <>; +chomp $num; + +my %seen; +my $res = 1; +while () { + if (exists $seen{$num}) { + $res = 0; + last; + } + $seen{$num}++; + $num = sum map {$_**2} split //, $num; + p $num; + sleep(1); + last if ($num == 1); +} + +say $res; \ No newline at end of file diff --git a/hash/minimum_index_sum_of_two_lists.pl b/hash/minimum_index_sum_of_two_lists.pl new file mode 100755 index 0000000..7da98a1 --- /dev/null +++ b/hash/minimum_index_sum_of_two_lists.pl @@ -0,0 +1,73 @@ +#!/usr/bin/perl +#Suppose Andy and Doris want to choose a restaurant for dinner, and +#they both have a list of favorite restaurants represented by strings. +#You need to help them find out their common interest with the +#least list index sum. If there is a choice tie between answers, +#output all of them with no order requirement. You could assume there always exists an answer. + +# Input: +# ["Shogun", "Tapioca Express", "Burger King", "KFC"] +# ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"] +# Output: ["Shogun"] +# Explanation: The only restaurant they both like is "Shogun". + +# Input: +# ["Shogun", "Tapioca Express", "Burger King", "KFC"] +# ["KFC", "Shogun", "Burger King"] +# Output: ["Shogun"] +# Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1). +use 5.016; +use DDP; +use List::Util qw/min/; +use Test::More; + +#строим 2 ассоциативных массива, по исходному. +#$arr1{"Shogun"} == "1", $arr1{"Tapioca Express"} +#$arr2{"KFC"} == "1", $arr2{"Shogun"} == 1 +#строим пересечение и в значении ставим сумму индексов. + +# my @arr1 = ("Shogun", "Tapioca Express", "Burger King", "KFC", "1"); +# my @arr2 = ("Burger King", "The Grill at Torrey Pines", "Tapioca Express", "Shogun", "1"); + +my (%arr1, %arr2); +for (1..10) { + $arr1{int rand 20} = 1; + $arr2{int rand 20} = 1; +} + +my @arr1 = keys %arr1; +my @arr2 = keys %arr2; +say '@arr1:'; +p @arr1; +say '@arr2:'; +p @arr2; +sub func { + my ($arr1, $arr2) = @_; + my @hash1 = map {$arr1->[$_] => $_} (0..$#$arr1); #O(N) + my @hash2 = map {$arr2->[$_] => $_} (0..$#$arr2); #O(N) + my %hash1 = @hash1; #O(1) + my %hash2 = @hash2; #O(1) + say '%hash1:'; + p %hash1; + say '%hash2:'; + p %hash2; + + my %interest = map {$_ => $hash1{$_} + $hash2{$_}} grep {exists $hash2{$_}} keys %hash1; #O(N log N) + say 'interest'; + p %interest; + + my %answer; + my $min = $#arr1 + $#arr2; #O(1) + for (keys %interest) { #O (N log N) + $answer{$interest{$_}} //= []; + push @{$answer{$interest{$_}}}, $_; + $min = $interest{$_} if $interest{$_} < $min; + } + say '%answer'; + p %answer; + say '$res:'; + my $res = $answer{$min}; #O(1) + p $res; +} + +func(\@arr1, \@arr2); \ No newline at end of file diff --git a/partition.py b/partition.py new file mode 100755 index 0000000..5236cc4 --- /dev/null +++ b/partition.py @@ -0,0 +1,16 @@ +#!/usr/bin/python3 +class Solution: + def arrayPairSum(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + return sum(sorted(nums)[0:len(nums) // 2]) + +s = Solution() + +print(s.arrayPairSum([1,2,3,4])) + + + + diff --git a/pascal.pl b/pascal.pl new file mode 100755 index 0000000..3e74579 --- /dev/null +++ b/pascal.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl +use 5.016; +use DDP; +# Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. + + +# In Pascal's triangle, each number is the sum of the two numbers directly above it. + +# Example: + +# Input: 5 +# Output: +# [ +# [1], +# [1,1], +# [1,2,1], +# [1,3,3,1], +# [1,4,6,4,1] +# ] + + + + +my $count = <>; +my $level = [1]; +sub next_level { + my $arr = shift; + my @res; + my $first = 0; + for my $i (@{$arr}) { + push @res, $i + $first; + $first = $i; + } + push @res, @{$arr}[-1]; + return \@res; +} +my @res; +push @res, $level; +for (1..$count - 1) { + $level = next_level($level); + push @res, $level; +} + +p @res; \ No newline at end of file diff --git a/pivot.pl b/pivot.pl new file mode 100755 index 0000000..ebec48e --- /dev/null +++ b/pivot.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl +# Given an array of integers nums, write a method that returns the "pivot" index of this array. + +# We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index. + +# If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index. + +# Example 1: + +# Input: +# nums = [1, 7, 3, 6, 5, 6] +# Output: 3 +# Explanation: +# The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3. +# Also, 3 is the first index where this occurs. + +# Example 2: + +# Input: +# nums = [1, 2, 3] +# Output: -1 +# Explanation: +# There is no index that satisfies the conditions in the problem statement. + +use 5.016; +use DDP; +use integer; +my @a = @{[0,1]}; +p @a; + +sub return_pivod(@) { + my @arr = @_; + return -1 unless (@arr); + + my $pivot = 0; + my $sum_left = 0; + my $sum_right = 0; + for (1..$#arr) { + $sum_right += $arr[$_]; + } + while ($sum_left != $sum_right) { + last if $pivot + 1 > $#arr; + $sum_left += $arr[$pivot]; + $sum_right -= $arr[$pivot + 1]; + $pivot++; + + } + return $pivot > $#arr ? -1 : $pivot; +} + +say return_pivod(@a); +reverse \ No newline at end of file diff --git a/pivot.py b/pivot.py new file mode 100755 index 0000000..8b103ae --- /dev/null +++ b/pivot.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 +nums = [0,1] +class Solution: + def pivotIndex(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + if (len(nums) == 0): + return -1 + pivot = 0 + sum_left = 0 + sum_right = sum(nums[1:]) + while(sum_left != sum_right): + if (pivot + 1 > len(nums) - 1): + break + sum_left += nums[pivot] + sum_right -= nums[pivot + 1] + pivot = pivot + 1 + + if (sum_right != sum_left): + pivot = -1 + return pivot + +p = Solution() +print (p.pivotIndex(nums)) diff --git a/reverse.pl b/reverse.pl new file mode 100755 index 0000000..25c24f0 --- /dev/null +++ b/reverse.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use 5.016; +use DDP; +my $str = <>; +# Write a function that takes a string as input and returns the string reversed. + +# Example: +# Given s = "hello", return "olleh". +chomp $str; +my @string = split //,$str; +p @string; + +my $i = 0; +my $j = $#string; +while ($i < $j) { + my $tmp = $string[$i]; + $string[$i] = $string[$j]; + $string[$j] = $tmp; + $i++; + $j--; +} + +p @string; \ No newline at end of file diff --git a/single_number.pl b/single_number.pl new file mode 100755 index 0000000..d1dbd6a --- /dev/null +++ b/single_number.pl @@ -0,0 +1,10 @@ +#!/usr/bin/perl +use 5.016; +use DDP; +my @arr1 = (1, 2, 2, 1); +my @arr2 = (2, 2); +my $res = 0; +my %hash; +@hash{@arr1} = (); +my @intersect = grep {!$hash{$_}++} @arr2; +p @intersect; \ No newline at end of file diff --git a/spiral.pl b/spiral.pl new file mode 100755 index 0000000..28f5313 --- /dev/null +++ b/spiral.pl @@ -0,0 +1,116 @@ +#!/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. + +# Example 1: + +# Input: +# [ +# [ 1, 2, 3 ], +# [ 4, 5, 6 ], +# [ 7, 8, 9 ] +# ] +# Output: [1,2,3,6,9,8,7,4,5] + +# Example 2: + +# Input: +# [ +# [1, 2, 3, 4], +# [5, 6, 7, 8], +# [9,10,11,12] +# ] +# Output: [1,2,3,4,8,12,11,10,9,5,6,7] + +my @matrix = ([]); + +my $A = $#matrix; +my $B = scalar @{$matrix[0]} - 1; +p $A; +p $B; +my @res; +if ($A > 0) { + push @res, @{$matrix[0]}; + my @second_row; + for (1..$A) { + push @second_row, $matrix[$_][$B]; + } + push @res, @second_row; +} else { + @res = @{$matrix[0]}; +} + +sub left { + my ($i, $j, $B) = @_; + my @ans; + for (my $k = $j - 1; $k >= $j - $B; $k--) { + push @ans, $matrix[$i][$k]; + } + return \@ans; +} + +sub right { + my ($i, $j, $B) = @_; + my @ans; + push @ans, $matrix[$i][$_] for $j + 1 .. $j + $B; + return \@ans; +} + +sub down { + my ($i, $j, $A) = @_; + my @ans; + push @ans, $matrix[$_][$j] for $i + 1 .. $i + $A; + return \@ans; +} + +sub up { + my ($i, $j, $A) = @_; + my @ans; + for (my $k = $i - 1; $k >= $i - $A; $k--) { + push @ans, $matrix[$k][$j]; + } + return \@ans; +} + + +my $i = $A; +my $j = $B; +my $state = "left"; +if ($A > 0 and $B > 0) { + while ($A != 0 or $B != 0) { + if ($state eq "right") { + last if $B == 0; + push @res, @{right($i, $j, $B)}; + $j += $B; + $A--; + $state = "down"; + next; + } + if ($state eq "down") { + last if $A == 0; + push @res, @{down($i, $j, $A)}; + $i += $A; + $B--; + $state = "left"; + next; + } + if ($state eq "left") { + last if $B == 0; + push @res, @{left($i, $j, $B)}; + $j -= $B; + $A--; + $state = "up"; + next; + } + if ($state eq "up") { + last if $A == 0; + push @res, @{up($i, $j, $A)}; + $i -= $A; + $B--; + $state = "right"; + next; + } + } +} +p @res; diff --git a/sublicates.pl b/sublicates.pl new file mode 100755 index 0000000..2a05eee --- /dev/null +++ b/sublicates.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use 5.016; +use DDP; +my @arr = (1,1,1,3,3,4,3,2,4,2); + +my %hash; + +my $res = 0; +for (@arr) { + $hash{$_}++; + if ($hash{$_} > 1) { + $res = 1; + last; + } +} +p %hash; +say $res; \ No newline at end of file diff --git a/sum2.pl b/sum2.pl new file mode 100755 index 0000000..7a0087d --- /dev/null +++ b/sum2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl +use 5.016; +my @numbers = (2,7,11,15); +my $target = 9; +my $l = 0; +my $r = $#numbers; + + +# Given an array of integers that is already sorted in ascending order, +#find two numbers such that they add up to a specific target number. + +# The function twoSum should return indices of the two numbers such that they add up to the target, +#where index1 must be less than index2. + +# Note: + +# Your returned answers (both index1 and index2) are not zero-based. +# You may assume that each input would have exactly one solution and you may not use the same element twice. + +# Example: + +# Input: numbers = [2,7,11,15], target = 9 +# Output: [1,2] +# Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2. + + +while ($l < $r) { + if ($numbers[$l] + $numbers[$r] == $target) { + print "$l $r\n"; + last; + } elsif ($numbers[$l] + $numbers[$r] < $target) { + $l++; + } elsif ($numbers[$l] + $numbers[$r] > $target) { + $r--; + } +} \ No newline at end of file diff --git a/sum2.py b/sum2.py new file mode 100755 index 0000000..2977b36 --- /dev/null +++ b/sum2.py @@ -0,0 +1,29 @@ +#!/usr/bin/python3 +class Solution: + def twoSum(self, numbers, target): + """ + :type numbers: List[int] + :type target: int + :rtype: List[int] + """ + i = 0 + j = len(numbers) - 1 + res = [] + for i in range(len(numbers)): + first = numbers[i] + find = target - first + i_bin = i + 1 + j_bin = len(numbers) - 1 + while (i_bin <= j_bin): + center = i_bin + (j_bin - i_bin) // 2 + if (numbers[center] == find): + return [i + 1, center + 1] + elif (numbers[center] < find): + i_bin = center + 1 + elif (numbers[center] > find): + j_bin = center - 1 + print(i_bin,find, numbers[center],j_bin) + + +s = Solution() +print(s.twoSum([2,3,4],6)) \ No newline at end of file