Logo

dev-resources.site

for different kinds of informations.

Step zero, step one

Published at
1/2/2025
Categories
perl
python
theweeklychallenge
Author
simongreennet
Categories
3 categories in total
perl
open
python
open
theweeklychallenge
open
Author
13 person written this
simongreennet
open
Step zero, step one

Weekly Challenge 302

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: Ones and Zeroes

Task

You are given an array of binary strings, @str, and two integers, $x and $y.

Write a script to return the size of the largest subset of @str such that there are at most $x 0ā€™s and $y 1ā€™s in the subset.

A set m is a subset of n if all elements of m are also elements of n.

My solution

Rather than calling the variables x and y, I've named them max_zeros and max_ones, as this is more meaningful. For the command line I take the last two values as max_zeros and max_ones with the rest of the values slurped into the s array.

For this task, I have the variable length that starts with the number of items in s down to one. For each length, I then use itertool's combination function to compute all unique combinations of length items, and store this in the subset variable.

For each subset, I join all the strings together, count the number of zeros and ones, and see if they are less than or equal to max_zeros and max_ones respectively. If they are, I return the length variable. If none is found, I return zero.

def ones_and_zeros(s: list, max_zeros: int, max_ones: int) -> int:
    for length in range(len(s), 0, -1):
        for subset in combinations(s, length):
            bits = ''.join(subset)
            if bits.count('0') <= max_zeros and bits.count('1') <= max_ones:
                return length

    return 0
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py 10 0001 111001 1 0 5 3
4

$ ./ch-1.py 10 0001 10 1 0 1 1
2
Enter fullscreen mode Exit fullscreen mode

Task 2: Step by Step

Task

You are given an array of integers, @ints.

Write a script to find the minimum positive start value such that step by step sum is never less than one.

My solution

Being really pedantic, the "minimum positive start value" for an list of positive integers is 0.000...01. However, the second example seems to imply that this number is actual an integer.

For this task, I start with two variables, min_value which is set as the first value of the ints list, and value which is zero. I then iterate over the ints list, and add that number to the value variable. If value is less than min_value, I update the min_value variable.

If min_value is greater than or equal to zero, I return 1. If not, I return 1 - min_value.

def step_by_step(ints: list) -> int:
    min_value = ints[0]
    value = 0

    for i in ints:
        value += i
        if min_value > value:
            min_value = value

    if min_value >= 0:
        return 1

    return 1 - min_value
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py 10 0001 111001 1 0 5 3
4

$ ./ch-1.py 10 0001 10 1 0 1 1
2

$ ./ch-2.py -3 2 -3 4 2
5

$ ./ch-2.py 1 2
1

$ ./ch-2.py 1 -2 -3
5
Enter fullscreen mode Exit fullscreen mode
perl Article's
30 articles in total
Favicon
How I used a named pipe to save memory and prevent crashes (in Perl)
Favicon
Perl šŸŖ Weekly #703 - Teach me some Perl!
Favicon
Earn with 3 digits
Favicon
Step zero, step one
Favicon
Perl šŸŖ Weekly #701 - Happier New Year!
Favicon
Hammering lists
Favicon
Nested beauty
Favicon
Catalyst Tricks: Map Request Parameters to a Model
Favicon
Rose::DB ORM and Perl
Favicon
Perl šŸŖ Weekly #699 - Happy birthday Perl
Favicon
The one about words
Favicon
Perl šŸŖ Weekly #702 - Perl Camel
Favicon
Perl šŸŖ Weekly #700 - White Camel Award 2024
Favicon
Maximally Indexed Indices (PWC 298)
Favicon
Solving the Weekly Challenge 302 Task 1: Ones and Zeroes in Python
Favicon
Solving the Weekly Challenge 302 Task 2: Step by Step in Python
Favicon
Habemus Perl Logo!
Favicon
Weekly Challenge 297
Favicon
Perl Weekly #696 - Perl 5 is Perl
Favicon
The Run-Length of Matchsticks (PWC 296)
Favicon
PWC 296 String Compression
Favicon
PWC 293 Similar Dominos Done Badly
Favicon
Jump, but Don't Break the Game (PWC 295)
Favicon
Ups and Downs, Beginnings and Ends (PWC 297)
Favicon
The Break Game
Favicon
How to create Mac GUI applications in SPVM?
Favicon
My Python Language Solution to Task 2: Nested Array from The Weekly Challenge 300
Favicon
My Python Language Solution to Task 1: Beautiful Arrangement from The Weekly Challenge 300
Favicon
My Python Language Solution to Task 1 from The Weekly Challenge 299
Favicon
Perl Weekly #698 - Perl v5.41.7

Featured ones: