Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit df10705

Browse files
Add 361_TallyProgram & 374_AdditivePersistence to DailyProgrammer
1 parent 5962d8f commit df10705

File tree

6 files changed

+350
-0
lines changed

6 files changed

+350
-0
lines changed

‎DailyProgrammer/Attempting/361_Tally-Program/361_TallyProgram.js

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Challenge #361 [Easy]: [Tally Program](https://www.reddit.com/r/dailyprogrammer/comments/8jcffg/20180514_challenge_361_easy_tally_program/)
2+
3+
#### Date: 14-May-2018
4+
5+
5 Friends (let's call them a, b, c, d and e) are playing a game and need to keep track of the scores. Each time someone scores a point, the letter of his name is typed in lowercase. If someone loses a point, the letter of his name is typed in uppercase. Give the resulting score from highest to lowest.
6+
7+
### Input Description
8+
A series of characters indicating who scored a point.
9+
10+
Examples:
11+
```
12+
abcde
13+
dbbaCEDbdAacCEAadcB
14+
```
15+
16+
### Output Description
17+
The score of every player, sorted from highest to lowest.
18+
19+
Examples:
20+
```
21+
a:1, b:1, c:1, d:1, e:1
22+
b:2, d:2, a:1, c:0, e:-2
23+
```
24+
25+
### Challenge Input
26+
```
27+
EbAAdbBEaBaaBBdAccbeebaec
28+
```
29+
30+
---
31+
32+
| Started | Last revisited | Completed |
33+
| ----------- | -------------- | ----------- |
34+
| 02-Feb-2018 | 02-Feb-2018 | dd-MMM-yyyy |
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// ===================================
2+
// Integer Complexity 1 - Comparative Solution
3+
// ===================================
4+
5+
// Based on strategy outlined in notes, we can quickly find factors
6+
7+
function intComplexityOne(number) {
8+
// default value: number * 1 ALWAYS = number
9+
// therefore we can start at number + 1 and work downwards if factors exist
10+
let lowestFactorSum = number + 1;
11+
12+
// Define upper bound for loop based on strategy in notes
13+
let factorUpperBound = Math.floor(Math.sqrt(number));
14+
// let factorUpperBound = 351364;
15+
16+
// take each integer between one and the factorUpperBound...
17+
for (let i = 1; i <= factorUpperBound; i++) {
18+
// ...and see if it divides evenly into the number.
19+
if (number % i === 0) {
20+
// if it does, take i's factor pair...
21+
let pair = number / i;
22+
23+
// ...add it to i to get the pair sum
24+
let pairSum = pair + i;
25+
26+
// if current pairSum is less than global lowestFactorSum...
27+
if (pairSum < lowestFactorSum) {
28+
// ...assign factorSum the value of current pairSum
29+
lowestFactorSum = pairSum;
30+
}
31+
}
32+
}
33+
// return the lowest factor sum;
34+
return lowestFactorSum;
35+
}
36+
37+
// TEST CASES
38+
// intComplexityOne(12); //=> 7 [CORRECT]
39+
// intComplexityOne(456); //=> 43 [CORRECT]
40+
// intComplexityOne(4567); //=> 4567 [CORRECT]
41+
// intComplexityOne(12345); //=> 838 [CORRECT]
42+
43+
// ANSWER
44+
// intComplexityOne(345678); //=> 3491 [CORRECT]
45+
46+
// BONUS
47+
// intComplexityOne(1234567891011); //=> 4695212 [NOPE]
48+
49+
// ===================================
50+
// Integer Complexity 1 - Old Array and Sorting Solution
51+
// ===================================
52+
53+
// Original solution that collected ALL sums, stored them, sorted them
54+
// and returned the first value of the stored sums array (would be lowest)
55+
56+
// Based on strategy outlined in notes, we can quickly find factors
57+
58+
function intComplexityOne(number) {
59+
// default value - we can assume number > 1
60+
// initial array to hold arrays of factor pairs
61+
let factorSumArr = [];
62+
63+
// Define upper bound for loop based on strategy in notes
64+
let factorUpperBound = Math.floor(Math.sqrt(number));
65+
66+
// take each integer between one and the factorUpperBound...
67+
for (let i = 1; i <= factorUpperBound; i++) {
68+
// ...and see if it divides evenly into the number.
69+
if (number % i === 0) {
70+
// if it does, take i's factor pair...
71+
let pair = number / i;
72+
73+
// ...add it to i...
74+
let pairSum = pair + i;
75+
76+
// ...and push that value to the factorSum array
77+
factorSumArr.push(pairSum);
78+
}
79+
}
80+
// at this point all factor pairs have been added together and pushed into the factorSum array
81+
// return the lowest number from this array of values
82+
let sorted = factorSumArr.sort((a, b) => {
83+
return a - b;
84+
});
85+
86+
// return factorSumArr[0];
87+
return sorted[0];
88+
}
89+
90+
intComplexityOne(12345); //=> 838 [CORRECT]
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Challenge #354 [Easy]: Integer Complexity - Notes
2+
3+
From the prompt:
4+
5+
> It's okay to use brute force by checking every possible value of B and C. You don't need to handle inputs larger than six digits.
6+
7+
Brute force sounds like fun but I think I have an idea regarding how to eliminate large numbers of test cases.
8+
9+
### Test Case: 12
10+
11+
Consider the test case of 12 and its factor pairs as shown below:
12+
13+
```
14+
1 x 12
15+
2 x 6
16+
3 x 4
17+
```
18+
19+
There are three pairs of factors, but technically, these pairs have pairs:
20+
21+
```
22+
4 x 3
23+
6 x 2
24+
12 x 1
25+
```
26+
27+
If we start testing for factors from 1, our upperbound will be the original number aka `1 x 12`.
28+
29+
However, when we move to 2, 6 presents itself as a factor. Since we are only dealing with positive integers, our upperbound has been reduced from 12 to 6 – the proof being that when we increment our base divisor by, we get a nice integer factor.
30+
31+
```
32+
BASE UPPER BOUND
33+
1 12 (original number)
34+
2 6
35+
3 4
36+
```
37+
38+
We can continue this pattern while (base <= divisor). After that, we begin mirroring results.
39+
40+
```
41+
BASE UPPER BOUND
42+
1 12 (original number)
43+
2 6
44+
3 4 (last case where base <= divisor)
45+
-------
46+
4 3 (first case of mirroring - further results meaningless)
47+
```
48+
49+
### Test Case: 36
50+
51+
Let's enumerate factor pairs and see where mirroring begins:
52+
53+
```
54+
BASE UPPER BOUND
55+
1 36 (original number)
56+
2 18
57+
3 12
58+
4 9
59+
6 6 (last case where base <= divisor)
60+
-------
61+
9 4 (first case of mirroring - further results meaningless)
62+
```
63+
64+
36 is a square so base 6 has divisor 6.
65+
66+
If we were to do some arbitrary division like `36 / 6.01`, the answer would be ~5.99, meaning that the base (6.01) is larger than the divisor (5.99). Thus, the upper bound of our brute force is equal to:
67+
68+
```js
69+
upperBound = Math.floor(Math.sqrt(inputNumber));
70+
```
71+
72+
Examples:
73+
74+
### Test Case: 18
75+
76+
```
77+
upperBound = Math.floor(Math.sqrt(18));
78+
= Math.floor(4.2)
79+
= 4
80+
81+
BASE UPPER BOUND
82+
1 18 (original number)
83+
2 9
84+
3 6 (last case where base <= divisor)
85+
-- 4 -- (mirror test upperBound)
86+
6 3 (first case of mirroring - further results meaningless)
87+
88+
upperBound test pass; 18 - 4 = 14 numbers saved from testing
89+
```
90+
91+
### Test Case: 140
92+
93+
```
94+
upperBound = Math.floor(Math.sqrt(140));
95+
= Math.floor(11.8)
96+
= 11
97+
98+
BASE UPPER BOUND
99+
1 140 (original number)
100+
2 70
101+
4 35
102+
5 28
103+
7 20
104+
10 14 (last case where base <= divisor)
105+
-- 11 -- (mirror test upperBound)
106+
14 10 (first case of mirroring - further results meaningless)
107+
108+
upperBound test pass; 140 - 11 = 129 numbers saved from testing
109+
```
110+
111+
Don't even need internal array of factors; just add them together on the spot.
112+
113+
### Sorting that array
114+
115+
```js
116+
factorSumArr.sort(
117+
// returns array with numbers sorted from low to high
118+
(a, b) => {
119+
return a - b;
120+
}
121+
);
122+
```
123+
124+
From here, we can just return the first entry
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Challenge #354 [Easy]: [Integer Complexity 1](https://www.reddit.com/r/dailyprogrammer/comments/83uvey/20180312_challenge_354_easy_integer_complexity_1/)
2+
3+
#### Date: 12-Mar-2018
4+
5+
#### Completed: Regular Solution only - Bonuses need work
6+
7+
Given a number A, find the smallest possible value of B+C, if B\*C = A. Here A, B, and C must all be positive integers. It's okay to use brute force by checking every possible value of B and C. You don't need to handle inputs larger than six digits. Post the return value for A = 345678 along with your solution.
8+
9+
For instance, given A = 12345 you should return 838. Here's why. There are four different ways to represent 12345 as the product of two positive integers:
10+
11+
```
12+
12345 = 1*12345
13+
12345 = 3*4115
14+
12345 = 5*2469
15+
12345 = 15*823
16+
```
17+
18+
The sum of the two factors in each case is:
19+
20+
```
21+
1*12345 => 1+12345 = 12346
22+
3*4115 => 3+4115 = 4118
23+
5*2469 => 5+2469 = 2474
24+
15*823 => 15+823 = 838
25+
```
26+
27+
The smallest sum of a pair of factors in this case is 838.
28+
29+
Examples:
30+
31+
```
32+
12 => 7
33+
456 => 43
34+
4567 => 4568
35+
12345 => 838
36+
```
37+
38+
The corresponding products are:
39+
40+
```
41+
12 = 3*4
42+
456 = 19*24
43+
4567 = 1*4567
44+
12345 = 15*823
45+
```
46+
47+
## Hint
48+
49+
Want to test whether one number divides evenly into another? This is most commonly done with the modulus operator (usually %), which gives you the remainder when you divide one number by another. If the modulus is 0, then there's no remainder and the numbers divide evenly. For instance, 12345 % 5 is 0, because 5 divides evenly into 12345.
50+
51+
### Optional bonus 1
52+
53+
Handle larger inputs efficiently. You should be able to handle up to 12 digits or so in about a second (maybe a little longer depending on your programming language). Find the return value for 1234567891011.
54+
55+
Hint: _how do you know when you can stop checking factors?_
56+
57+
### Optional bonus 2
58+
59+
Efficiently handle very large inputs whose prime factorization you are given. For instance, you should be able to get the answer for 6789101112131415161718192021 given that its prime factorization is:
60+
61+
6789101112131415161718192021 = 3*3*3*53*79*1667*20441*19646663*89705489
62+
In this case, you can assume you're given a list of primes instead of the number itself. (To check your solution, the output for this input ends in 22.)
63+
64+
---
65+
66+
| Started | Last revisited | Completed |
67+
| ----------- | -------------- | ----------- |
68+
| 07-Sep-2018 | 07-Sep-2018 | dd-MMM-yyyy |
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Challenge #374 [Easy]: [Additive Persistence](https://www.reddit.com/r/dailyprogrammer/comments/akv6z4/20190128_challenge_374_easy_additive_persistence/)
2+
3+
#### Date: 29-Jan-2019
4+
5+
#### Completed: Regular Solution only - Bonuses need work
6+
7+
Inspired by this tweet, today's challenge is to calculate the additive persistence of a number, defined as how many loops you have to do summing its digits until you get a single digit number. Take an integer N:
8+
9+
- Add its digits
10+
- Repeat until the result has 1 digit
11+
12+
The total number of iterations is the additive persistence of N.
13+
14+
Your challenge today is to implement a function that calculates the additive persistence of a number.
15+
16+
### Examples
17+
```
18+
13 -> 1
19+
1234 -> 2
20+
9876 -> 2
21+
199 -> 3
22+
```
23+
24+
### Bonus
25+
26+
The really easy solution manipulates the input to convert the number to a string and iterate over it. Try it without making the number a strong, decomposing it into digits while keeping it a number.
27+
28+
On some platforms and languages, if you try and find ever larger persistence values you'll quickly learn about your platform's big integer interfaces (e.g. 64 bit numbers).
29+
30+
---
31+
32+
| Started | Last revisited | Completed |
33+
| ----------- | -------------- | ----------- |
34+
| 02-Feb-2018 | 02-Feb-2018 | dd-MMM-yyyy |

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /