https://www.codementor.io/sumit12dec/python-tricks-for-beginners-du107t193, '10 Neat Python Tricks' is the title of this link. My perl solutions to these tricks can be seen below. Interestingly in many cases perl solution looks more obvious than python solution. Any comment is welcome.
Update: Confusingly edited IX and X are corrected.
Update 2: X fixed
Update 3: I updated
I. Reversing a string in Python ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYTHON a = "codementor" print "Reverse is", a[::-1] PERL my $a ="GeeksForGeeks" say 'Reverse is '. reverse $a; ---------------------------------------------------------------------- +---- II. Transposing a Matrix ^^^^^^^^^^^^^^^^^^^^^^^^ PYTHON mat = [[1, 2, 3], [4, 5, 6]] zip(*mat) PERL use List::MoreUtils qw/each_arrayref/; my @l = ([1, 2, 3], [4, 5, 6]); my @a; my $iter = each_arrayref(@l); while (my @t = $iter->()) { push @a, \@t; } ---------------------------------------------------------------------- +---- III. Store all three values of the list in 3 new variables ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYTHON a = [1, 2, 3] x, y, z = a PERL my @a = qw/1 2 3/; my ($x, $y, $z) = @a; ---------------------------------------------------------------------- +---- IV. Create a single string from all the elements in list below ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYTHON a = ["Code", "mentor", "Python", "Developer"] print " ".join(a) PERL my @a = qw/Code mentor Python Developer/; say "@a"; ---------------------------------------------------------------------- +---- V. Write a Python code to print ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYTHON list1 = ['a', 'b', 'c', 'd'] list2 = ['p', 'q', 'r', 's'] for x, y in zip(list1,list2): print x, y PERL my @list1 = qw/a b c d/; my @list2 = qw/p q r s/; for (my $i=0; $i<@list1; ++$i) { say "$list[$i] $list2[$i]"; } ---------------------------------------------------------------------- +---- VI. Swap two numbers with one line of code ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYTHON a = 7 b = 5 b, a = a, b PERL my ($a, $b) = (7, 5); ($b, $a) = ($a, $b); ---------------------------------------------------------------------- +---- VII. Print "codecodecodecode mentormentormentormentormentor" without u +sing loops ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +^^^^^^^^^^ PYTHON print "code" * 4 + ' ' + "mentor" * 5 PERL say 'code' x 4, ' ', 'mentor' x 5; ---------------------------------------------------------------------- +---- VIII. Convert it to a single list without using any loops ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYTHON import itertools a = [[1, 2], [3, 4], [5, 6]] list(itertools.chain.from_iterable(a)) PERL my @a = ([1, 2], [3, 4], [5, 6]); @l = map { @$_ } @a; ---------------------------------------------------------------------- +---- IX. Checking if two words are anagrams ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYTHON from collections import Counter def is_anagram(str1, str2): return Counter(str1) == Counter(str2) PERL sub is_anagram { return (join('', sort split('', $_[0])) eq join('', sort split('', $_[1]))) ? 'True' : 'False'; } ---------------------------------------------------------------------- +---- X. Taking a string input ^^^^^^^^^^^^^^^^^^^^^^^^ For example "1 2 3 4" and return [1, 2, 3, 4] Remember list being returned has integers in it. Don't use more than one line of code. PYTHON result = map(lambda x:int(x) ,raw_input().split()) PERL my @r = split /\s+/, <STDIN>;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Python tricks
by hippo (Archbishop) on Dec 18, 2018 at 12:28 UTC | |
by pme (Monsignor) on Dec 18, 2018 at 15:05 UTC | |
by xiaoyafeng (Deacon) on Dec 19, 2018 at 07:21 UTC | |
by syphilis (Archbishop) on Dec 19, 2018 at 07:37 UTC | |
by pme (Monsignor) on Dec 19, 2018 at 08:29 UTC | |
Re: Python tricks
by tybalt89 (Monsignor) on Dec 18, 2018 at 17:52 UTC | |
Re: Python tricks
by markong (Pilgrim) on Dec 18, 2018 at 12:49 UTC | |
by pme (Monsignor) on Dec 19, 2018 at 08:42 UTC | |
Re: Python tricks
by soonix (Chancellor) on Dec 18, 2018 at 12:55 UTC | |
by pme (Monsignor) on Dec 18, 2018 at 15:08 UTC | |
Re: Python tricks
by Eily (Monsignor) on Dec 18, 2018 at 14:21 UTC | |
Re: Python tricks
by thechartist (Monk) on Mar 04, 2019 at 21:49 UTC | |
by etj (Priest) on May 02, 2022 at 17:22 UTC | |
Re: Python tricks
by parv (Parson) on Dec 18, 2018 at 13:08 UTC | |
Re: Python tricks
by rsFalse (Chaplain) on Mar 04, 2019 at 13:22 UTC | |
Re: Python tricks
by rsFalse (Chaplain) on Mar 04, 2019 at 13:38 UTC |