This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Better sollution | |
| def main(): | |
| v = (1, 2, 3, 4, 5) | |
| r = l = 1 | |
| print(bounded_ratio(v, l, r)) | |
| def bounded_ratio(a, l, r): | |
| values = [] | |
| for i, v in enumerate(a): | |
| values = values + [(i + 1) * x == v for x in range(l, r + 1)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Shorter solution | |
| def bounded_ratio(a, l, r): | |
| for i, v in enumerate(a): | |
| for x in range(l, r + 1): | |
| yield (i + 1) * x == v | |
| v = (1, 2, 3, 4, 5) | |
| r = l = 1 | |
| print(list(bounded_ratio(v, l, r))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const boundedRatio = (a, l, r) => { | |
| return a.map((v, i) => { | |
| for (let x = l; x <= r; x++) { | |
| return (i + 1) * x == v; | |
| } | |
| }) | |
| } | |
| const v = [1, 2, 3, 4, 5] | |
| const r = l = 1 | |
| console.log(boundedRatio(v, l, r)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*/We 'd like to contact partners with | |
| offices within 100km of central London (coordinates 51.515419, -0.141099) to invite them out for a meal. | |
| Write a NodeJS/JavaScript program that reads our list of partners (download partners.json here) | |
| and outputs the company names and addresses of matching partners (with offices within 100km) | |
| sorted by company name (ascending). | |
| You can use the first formula from this Wikipedia article to calculate distance. | |
| Don 't forget to convert degrees to radians! Your program should be fully tested too. | |
| */ | |
| const fs = require('fs'); | |
| const data = fs.readFileSync('partners.json'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*Write a function called deepClone which takes an object and creates a copy of it. e.g. | |
| {name: "Paddy", address: {town: "Lerum", country: "Sweden"}} -> {name: "Paddy", address: {town: "Lerum", country: "Sweden"}}*/ | |
| function deepClone(obj){ | |
| var obj2 = obj; | |
| return obj2; | |
| } | |
| var Paddy = {name: "Paddy", address: {town: "Lerum", country: "Sweden"}}; |