From 579c05eadfc83acd0b2f5a65567ec781f8e6f317 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 4 Sep 2024 10:59:07 +0100 Subject: Add Python solution to challenge 052 --- challenge-052/paulo-custodio/python/ch-1.py | 21 +++++++++++++ challenge-052/paulo-custodio/python/ch-2.py | 48 +++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 challenge-052/paulo-custodio/python/ch-1.py create mode 100644 challenge-052/paulo-custodio/python/ch-2.py diff --git a/challenge-052/paulo-custodio/python/ch-1.py b/challenge-052/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..7786282a38 --- /dev/null +++ b/challenge-052/paulo-custodio/python/ch-1.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +# Challenge 052 +# +# TASK #1 +# Stepping Numbers +# Write a script to accept two numbers between 100 and 999. It should then print +# all Stepping Numbers between them. +# +# A number is called a stepping number if the adjacent digits have a difference +# of 1. For example, 456 is a stepping number but 129 is not. + +import sys + +STEPPING_NUMS = [123, 234, 345, 456, 567, 678, 789] + +start = int(sys.argv[1]) +end = int(sys.argv[2]) +for n in STEPPING_NUMS: + if n >= start and n <= end: + print(n) diff --git a/challenge-052/paulo-custodio/python/ch-2.py b/challenge-052/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..1bb6b70bed --- /dev/null +++ b/challenge-052/paulo-custodio/python/ch-2.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 + +# Challenge 052 +# +# TASK #2 +# Lucky Winner +# Suppose there are following coins arranged on a table in a line in random +# order. +# +# Pound1, 50p, 1p, 10p, 5p, 20p, Pound2, 2p +# +# Suppose you are playing against the computer. Player can only pick one coin +# at a time from either ends. Find out the lucky winner, who has the larger +# amounts in total? + +from random import shuffle + +coins = [100, 50, 1, 10, 5, 20, 200, 2] +shuffle(coins) + +human = 0 +computer = 0 + +while len(coins) > 0: + # human + draw = "" + while draw != "b" and draw != "e": + draw = input("Coins: "+" ".join([str(x) for x in coins])+". draw (b/e)? ") + if draw == "b": + human += coins[0] + coins = coins[1:] + else: + human += coins[-1] + coins = coins[:-1] + + # computer + if len(coins) > 0: + if coins[0] >= coins[-1]: + computer += coins[0] + coins = coins[1:] + else: + computer += coins[-1] + coins = coins[:-1] + +if human > computer: + print(f"You WIN ({human}/{computer})") +else: + print(f"You LOOSE ({human}/{computer})") -- cgit From 9f9759bf51628e5c9216166870965e0ae0357391 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 4 Sep 2024 21:34:14 +0100 Subject: Add Python solution to challenge 284 --- challenge-284/paulo-custodio/perl/ch-2.pl | 2 +- challenge-284/paulo-custodio/python/ch-1.py | 44 ++++++++++++++++++++++++++ challenge-284/paulo-custodio/python/ch-2.py | 48 +++++++++++++++++++++++++++++ challenge-284/paulo-custodio/t/test-2.yaml | 6 ++-- 4 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 challenge-284/paulo-custodio/python/ch-1.py create mode 100644 challenge-284/paulo-custodio/python/ch-2.py diff --git a/challenge-284/paulo-custodio/perl/ch-2.pl b/challenge-284/paulo-custodio/perl/ch-2.pl index 6bb9662a88..4813ce73f0 100644 --- a/challenge-284/paulo-custodio/perl/ch-2.pl +++ b/challenge-284/paulo-custodio/perl/ch-2.pl @@ -43,4 +43,4 @@ while (@list2) { } push @output, sort {$a <=> $b} @list1; -say "@output"; +say join ", ", @output; diff --git a/challenge-284/paulo-custodio/python/ch-1.py b/challenge-284/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..cebd0835de --- /dev/null +++ b/challenge-284/paulo-custodio/python/ch-1.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +# Challenge 284 +# +# Task 1: Lucky Integer +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to find the lucky integer if found otherwise return -1. If +# there are more than one then return the largest. +# +# A lucky integer is an integer that has a frequency in the array equal to +# its value. +# +# Example 1 +# +# Input: @ints = (2, 2, 3, 4) +# Output: 2 +# +# Example 2 +# +# Input: @ints = (1, 2, 2, 3, 3, 3) +# Output: 3 +# +# Example 3 +# +# Input: @ints = (1, 1, 1, 3) +# Output: -1 + +import sys + +ints = [int(x) for x in sys.argv[1:]] +count = {} +for x in ints: + if x in count: + count[x] += 1 + else: + count[x] = 1 +lucky = sorted(list(filter(lambda x:x == count[x], ints)))[::-1] +if len(lucky) > 0: + print(lucky[0]) +else: + print(-1) diff --git a/challenge-284/paulo-custodio/python/ch-2.py b/challenge-284/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..b51af0f0ac --- /dev/null +++ b/challenge-284/paulo-custodio/python/ch-2.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 + +# Challenge 284 +# +# Task 2: Relative Sort +# Submitted by: Mohammad Sajid Anwar +# +# You are given two list of integers, @list1 and @list2. The elements in the +# @list2 are distinct and also in the @list1. +# +# Write a script to sort the elements in the @list1 such that the relative +# order of items in @list1 is same as in the @list2. Elements that is missing +# in @list2 should be placed at the end of @list1 in ascending order. +# Example 1 +# +# Input: @list1 = (2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5) +# @list2 = (2, 1, 4, 3, 5, 6) +# Ouput: (2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9) +# +# Example 2 +# +# Input: @list1 = (3, 3, 4, 6, 2, 4, 2, 1, 3) +# @list2 = (1, 3, 2) +# Ouput: (1, 3, 3, 3, 2, 2, 4, 4, 6) +# +# Example 3 +# +# Input: @list1 = (3, 0, 5, 0, 2, 1, 4, 1, 1) +# @list2 = (1, 0, 3, 2) +# Ouput: (1, 1, 1, 0, 0, 3, 2, 4, 5) + +import sys + +args = (" ".join(sys.argv[1:])).split(',') +list1 = [int(x) for x in args[0].split()] +list2 = [int(x) for x in args[1].split()] + +output = [] +while len(list2) > 0: + n = list2[0] + list2 = list2[1:] + for x in filter(lambda x:x == n, list1): + output.append(x) + list1 = list(filter(lambda x:x != n, list1)) +for x in sorted(list1): + output.append(x) + +print(", ".join([str(x) for x in output])) diff --git a/challenge-284/paulo-custodio/t/test-2.yaml b/challenge-284/paulo-custodio/t/test-2.yaml index f4d2db7f63..4d0548ddd5 100644 --- a/challenge-284/paulo-custodio/t/test-2.yaml +++ b/challenge-284/paulo-custodio/t/test-2.yaml @@ -2,14 +2,14 @@ cleanup: args: 2 3 9 3 1 4 6 7 2 8 5 , 2 1 4 3 5 6 input: - output: 2 2 1 4 3 3 5 6 7 8 9 + output: 2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9 - setup: cleanup: args: 3 3 4 6 2 4 2 1 3 , 1 3 2 input: - output: 1 3 3 3 2 2 4 4 6 + output: 1, 3, 3, 3, 2, 2, 4, 4, 6 - setup: cleanup: args: 3 0 5 0 2 1 4 1 1 , 1 0 3 2 input: - output: 1 1 1 0 0 3 2 4 5 + output: 1, 1, 1, 0, 0, 3, 2, 4, 5 -- cgit