aboutsummaryrefslogtreecommitdiff
path: root/challenge-251/jeanluc2020/python/ch-1.py
blob: 9f0b248134c606e9c916638b2c50b0b2dc586547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python3
# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/#TASK1
#
# Task 1: Concatenation Value
# ===========================
#
# You are given an array of integers, @ints.
#
# Write a script to find the concatenation value of the given array.
#
# The concatenation of two numbers is the number formed by concatenating their
# numerals.
#
## For example, the concatenation of 10, 21 is 1021.
## The concatenation value of @ints is initially equal to 0.
## Perform this operation until @ints becomes empty:
##
## If there exists more than one number in @ints, pick the first element
## and last element in @ints respectively and add the value of their
## concatenation to the concatenation value of @ints, then delete the
## first and last element from @ints.
##
## If one element exists, add its value to the concatenation value of
## @ints, then delete it.
#
## Example 1
##
## Input: @ints = (6, 12, 25, 1)
## Output: 1286
##
## 1st operation: concatenation of 6 and 1 is 61
## 2nd operation: concaternation of 12 and 25 is 1225
##
## Concatenation Value => 61 + 1225 => 1286
#
## Example 2
##
## Input: @ints = (10, 7, 31, 5, 2, 2)
## Output: 489
##
## 1st operation: concatenation of 10 and 2 is 102
## 2nd operation: concatenation of 7 and 2 is 72
## 3rd operation: concatenation of 31 and 5 is 315
##
## Concatenation Value => 102 + 72 + 315 => 489
#
## Example 3
##
## Input: @ints = (1, 2, 10)
## Output: 112
##
## 1st operation: concatenation of 1 and 10 is 110
## 2nd operation: only element left is 2
##
## Concatenation Value => 110 + 2 => 112
#
############################################################
##
## discussion
##
############################################################
#
# Starting with a sum of 0, we call a function that just concatenates
# the first and last elements of @ints and adds that number to the
# current sum, then we call the function recursively with the new
# current sum and the remainder of the array. Of course handle the
# case where there is only one element in the array by returning the
# current sum + the last element, and the case of an empty array by
# returning the current sum.

def concatenation_value_(current: int, ints: list) -> int:
    if len(ints) == 0:
        return current
    if len(ints) == 1:
        return current + ints[0]
    value = str(ints[0]) + str(ints[-1])
    return concatenation_value_(current + int(value), ints[1:-1])

def concatenation_value(ints: list) -> None:
    print("Input: (", ", ".join([str(x) for x in ints]), ")")
    result = concatenation_value_(0, ints)
    print(f"Output: {result}")

concatenation_value([6, 12, 25, 1])
concatenation_value([10, 7, 31, 5, 2, 2])
concatenation_value([1, 2, 10])