Publications tagged "python"

Lambdas & Printf

Task I Ok, another great week and I was glad that it did not require too much effort on my part, as I’ve started participating in AoC, AoCyber, and also doing Gabor Szabo’s 2022 December CI Challenge. Thus without further ado, let’s get into the code. #!/usr/bin/env python3 def bit_str(n): limit = 1 << n for i in range(limit): value = i width = f'0{n}' print(f'{value:{width}b}') if __name__ == "__main__": bit_str(2) print('\n') bit_str(3) It was a very straightforward implementation using Python & Go’s mini string formatting language.


This week’s first task is almost a continuation of task II. Task 1: Binary Flip You are given a positive integer, $n. Write a script to find the binary flip. Example 1 Input: $n = 5 Output: 2 First find the binary equivalent of the given integer, 101. Then flip the binary digits 0 -> 1 and 1 -> 0 and we get 010. So Binary 010 => Decimal 2. Example 2 Input: $n = 4 Output: 3 Decimal 4 = Binary 100 Flip 0 -> 1 and 1 -> 0, we get 011.

Reduce & Backtrack

Perl Weekly Challenge 191 Task I Another week, another opportunity to participate in the weekly Perl Challenge. The first task was self explanatory if you read the code. With use of reduce() from functools, it makes for very efficient and readable code. The logic is simple to figure out, since the sum of the n items in the list cannot be greater than the largest item. For example: Take the following list: [1, 2, 3, 4].

DFS & Backtracking

Perl Weekly Challenge 190 Task 1: Capital Detection You are given a string with alphabetic characters only: A..Z and a..z. Write a script to find out if the usage of Capital is appropriate if it satisfies at least one of the following rules: Only first letter is capital and all others are small. Every letter is small. Every letter is capital. Example 1 Input: $s = 'Perl' Output: 1 Example 2 Input: $s = 'TPF' Output: 1 Example 3 Input: $s = 'PyThon' Output: 0 Example 4 Input: $s = 'raku' Output: 1 This one was right down to the point, one simply takes the challenge rules, perform some structural pattern matching and display the result.

Back to top