Hammering lists
Weekly Challenge 301
Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. It's a great way for us all to practice some coding.
Challenge, My solutions
Task 1: Largest Number
Task
You are given a list of positive integers, @ints.
Write a script to arrange all the elements in the given list such that they form the largest number and return it.
My solution
Maybe I'm overthinking it, but this isn't as easy at it sounds.
One option would be to compute all permutations, and see which number is largest. However this gets resource intensive as we add more integers. If I had thirteen integers, there are over 6 billion permutations. So I rule this out as a possible solution.
So the obvious thing to do is to sort the integers, combine them and output the result. As Python treats strings and integers differently, I need to convert the sorted list to strings, join them and convert that back to an integer.
def largest_number(ints: list) -> int: sorted_ints = sorted(ints, key=cmp_to_key(number_sort), reverse=True) return int(''.join(map(str, sorted_ints)))
When it comes to sorting, this is a little complicated. In the second supplied example, we can see that 3, 30, and 34 are all given integers. For this, I know the largest number is obtained by ordering the items (highest to lowest) 34, 3 and 30.
For my number_sort function, I convert the integers to strings, s1 and s2. I then have the integer c1 which is the concatenation of s1 and s2, while c2 is the concatenation of s2 and s1.
If c1 is less than than c2, I return -1. If it is larger, I return 1. If they are the same, I return 0. The sorted function uses this information to sort the list as required.
def number_sort(i1: int, i2: int) -> int: s1 = str(i1) s2 = str(i2) c1 = int(s1 + s2) c2 = int(s2 + s1) if c1 < c2: return -1 if c1 > c2: return 1 return 0
The Perl code is much simpler :)
sub number_sort() { return "$a$b" <=> "$b$a"; }
Examples
$ ./ch-1.py 20 3 320 $ ./ch-1.py 3 30 34 5 9 9534330
Task 2: Hamming Distance
Task
You are given an array of integers, @ints.
Write a script to return the sum of Hamming distances between all the pairs of the integers in the given array of integers.
The Hamming distance between two integers is the number of places in which their binary representations differ.
My solution
In the previous task I mentioned how Python treats integers and strings as different types. One of the advantages of Perl is that we are told that for all intents and purposes, we don't need to worry about typing of variables. Even though internally they are stored differently, Perl knows what to do.
In Perl 5.10 and Perl 5.16 (which is where I did most of my Perl development), there are two notable exceptions. One is the JSON module, which will output "10" for a string and 10 for an integer.
The other is bitwise operations. From the perlop page, 105 | 150 (two integers) is 255, while "105" | "150" (two strings) is 155.
So I was pleasantly surprised when I read the perlop page again to see that this has been sorted out in later version of Perl. It now has the bitwise feature which is experimental in Perl 5.22, and available in Perl 5.28. This ensures that bitwise operators always treat the values as an integer, and string-based bitwise has new operators.
Anyway, back to the task at hand. For this I compute all combinations of two integers. For each combination, I perform an XOR (exclusive or) of the two values, convert it to binary and count the number of 1s in the binary representation.
def largest_number(ints: list) -> int: sorted_ints = sorted(ints, key=cmp_to_key(number_sort), reverse=True) return int(''.join(map(str, sorted_ints)))
Examples
def number_sort(i1: int, i2: int) -> int: s1 = str(i1) s2 = str(i2) c1 = int(s1 + s2) c2 = int(s2 + s1) if c1 < c2: return -1 if c1 > c2: return 1 return 0
The above is the detailed content of Hammering lists. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
