Challenge: Connecting Towns

Subdomeniu: Fundamentals (fundamentals)

Scor cont: 10.0 / 10

Submission status: Accepted

Submission score: 1.0

Submission ID: 464719291

Limbaj: python3

Link challenge: https://www.hackerrank.com/challenges/connecting-towns/problem

Cerinta

Cities on a map are connected by a number of roads.  The number of roads between each city is in an array and city $0$ is the starting location.  The number of roads from city $0$ to city $1$ is the first value in the array, from city $1$ to city $2$ is the second, and so on.  

How many paths are there from city $0$ to the last city in the list, modulo $1234567$?

**Example**  
$n = 4$  
$routes = [3, 4, 5]$  

There are $3$ roads to city $1$, $4$ roads to city $2$ and $5$ roads to city $3$.  The total number of roads is $3 \times 4 \times 5 \mod 1234567 = 60$. 

**Note**  
Pass all the towns T<sub>i</sub> for i=1 to n-1 in numerical order to reach T<sub>n</sub>.  

**Function Description**  

Complete the *connectingTowns* function in the editor below.  

*connectingTowns* has the following parameters:  

- *int n:* the number of towns  
- *int routes[n-1]:* the number of routes between towns  

**Returns**  

- *int:* the total number of routes, modulo 1234567. 

**Input Format**  
The first line contains an integer T, T test-cases follow.   

Each test-case has 2 lines.  
  The first line contains an integer N (the number of towns).   
  The second line contains N - 1 space separated integers where the i<sup>th</sup> integer denotes the number of routes, N<sub>i</sub>, from the town T<sub>i</sub> to T<sub>i+1</sub>  

**Constraints**  
1 <= T<=1000<br>
2< N <=100<br>
1 <= routes[i] <=1000

**Sample Input**  

    2
    3
    1 3
    4
    2 2 2

**Sample Output**  

    3
    8

**Explanation**  
Case 1: 1 route from T<sub>1</sub> to T<sub>2</sub>, 3 routes from T<sub>2</sub> to T<sub>3</sub>, hence only 3 routes.  
Case 2: There are 2 routes from each city to the next, hence 2 * 2 * 2 = 8.

Cod sursa

#!/bin/python3

import math
import os
import random
import re
import sys

def connectingTowns(n, routes):
    # Write your code here
    res = 1
    for i in routes:
        res = res * i % 1234567
    return res
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input().strip())

    for t_itr in range(t):
        n = int(input().strip())

        routes = list(map(int, input().rstrip().split()))

        result = connectingTowns(n, routes)

        fptr.write(str(result) + '\n')

    fptr.close()
HackerRank Fundamentals – Connecting Towns