Naismith's rule
Clash Royale CLAN TAG#URR8PPP
up vote
13
down vote
favorite
Naismith's rule helps to work out the length of time needed for a walk or hike, given the distance and ascent.
Given a non-empty list of the altitude at points evenly spaced along a path and the total distance of that path in metres, you should calculate the time needed according to Naismith's rule.
Naismith's rule is that you should allow one hour for every five kilometres, plus an additional hour for every 600 metres of ascent.
Input must be taken in metres, which is guaranteed to consist of non-negative integers, and output should consistently be either hours or minutes (but not both), and must be able to give decimal numbers where applicable (floating point inaccuracies are OK).
For example, given:
[100, 200, 400, 200, 700, 400], 5000
For the first two elements [100, 200]
you have 100 metres of ascent which is 10 minutes. With [200, 400]
you have 200 metres of ascent which is 20 minutes, [400, 200]
is not ascending so no time is added for that. [200, 700]
is 500 metres of ascent which is 50 minutes, and finally [700, 400]
is not ascending. One extra hour is added for the distance of five kilometres. This totals to 140 minutes or 2.333... hours.
Test Cases
[0, 600] 2500 -> 1.5 OR 90
[100, 200, 300, 0, 100, 200, 300] 10000 -> 2.8333... OR 170
[40, 5, 35] 1000 -> 0.25 OR 15
[604] 5000 -> 1 OR 60
[10, 10, 10] 2000 -> 0.4 OR 24
code-golf number
add a comment |Â
up vote
13
down vote
favorite
Naismith's rule helps to work out the length of time needed for a walk or hike, given the distance and ascent.
Given a non-empty list of the altitude at points evenly spaced along a path and the total distance of that path in metres, you should calculate the time needed according to Naismith's rule.
Naismith's rule is that you should allow one hour for every five kilometres, plus an additional hour for every 600 metres of ascent.
Input must be taken in metres, which is guaranteed to consist of non-negative integers, and output should consistently be either hours or minutes (but not both), and must be able to give decimal numbers where applicable (floating point inaccuracies are OK).
For example, given:
[100, 200, 400, 200, 700, 400], 5000
For the first two elements [100, 200]
you have 100 metres of ascent which is 10 minutes. With [200, 400]
you have 200 metres of ascent which is 20 minutes, [400, 200]
is not ascending so no time is added for that. [200, 700]
is 500 metres of ascent which is 50 minutes, and finally [700, 400]
is not ascending. One extra hour is added for the distance of five kilometres. This totals to 140 minutes or 2.333... hours.
Test Cases
[0, 600] 2500 -> 1.5 OR 90
[100, 200, 300, 0, 100, 200, 300] 10000 -> 2.8333... OR 170
[40, 5, 35] 1000 -> 0.25 OR 15
[604] 5000 -> 1 OR 60
[10, 10, 10] 2000 -> 0.4 OR 24
code-golf number
The test cases outputs all have whole-minute results, is that intentional? Are inputs like[10], 5125
or[10, 25, 55], 1000
valid and required to be handled?
â sundar
20 hours ago
@sundar Yes, they should.
â Okx
14 hours ago
add a comment |Â
up vote
13
down vote
favorite
up vote
13
down vote
favorite
Naismith's rule helps to work out the length of time needed for a walk or hike, given the distance and ascent.
Given a non-empty list of the altitude at points evenly spaced along a path and the total distance of that path in metres, you should calculate the time needed according to Naismith's rule.
Naismith's rule is that you should allow one hour for every five kilometres, plus an additional hour for every 600 metres of ascent.
Input must be taken in metres, which is guaranteed to consist of non-negative integers, and output should consistently be either hours or minutes (but not both), and must be able to give decimal numbers where applicable (floating point inaccuracies are OK).
For example, given:
[100, 200, 400, 200, 700, 400], 5000
For the first two elements [100, 200]
you have 100 metres of ascent which is 10 minutes. With [200, 400]
you have 200 metres of ascent which is 20 minutes, [400, 200]
is not ascending so no time is added for that. [200, 700]
is 500 metres of ascent which is 50 minutes, and finally [700, 400]
is not ascending. One extra hour is added for the distance of five kilometres. This totals to 140 minutes or 2.333... hours.
Test Cases
[0, 600] 2500 -> 1.5 OR 90
[100, 200, 300, 0, 100, 200, 300] 10000 -> 2.8333... OR 170
[40, 5, 35] 1000 -> 0.25 OR 15
[604] 5000 -> 1 OR 60
[10, 10, 10] 2000 -> 0.4 OR 24
code-golf number
Naismith's rule helps to work out the length of time needed for a walk or hike, given the distance and ascent.
Given a non-empty list of the altitude at points evenly spaced along a path and the total distance of that path in metres, you should calculate the time needed according to Naismith's rule.
Naismith's rule is that you should allow one hour for every five kilometres, plus an additional hour for every 600 metres of ascent.
Input must be taken in metres, which is guaranteed to consist of non-negative integers, and output should consistently be either hours or minutes (but not both), and must be able to give decimal numbers where applicable (floating point inaccuracies are OK).
For example, given:
[100, 200, 400, 200, 700, 400], 5000
For the first two elements [100, 200]
you have 100 metres of ascent which is 10 minutes. With [200, 400]
you have 200 metres of ascent which is 20 minutes, [400, 200]
is not ascending so no time is added for that. [200, 700]
is 500 metres of ascent which is 50 minutes, and finally [700, 400]
is not ascending. One extra hour is added for the distance of five kilometres. This totals to 140 minutes or 2.333... hours.
Test Cases
[0, 600] 2500 -> 1.5 OR 90
[100, 200, 300, 0, 100, 200, 300] 10000 -> 2.8333... OR 170
[40, 5, 35] 1000 -> 0.25 OR 15
[604] 5000 -> 1 OR 60
[10, 10, 10] 2000 -> 0.4 OR 24
code-golf number
edited yesterday
asked yesterday
Okx
11.9k27100
11.9k27100
The test cases outputs all have whole-minute results, is that intentional? Are inputs like[10], 5125
or[10, 25, 55], 1000
valid and required to be handled?
â sundar
20 hours ago
@sundar Yes, they should.
â Okx
14 hours ago
add a comment |Â
The test cases outputs all have whole-minute results, is that intentional? Are inputs like[10], 5125
or[10, 25, 55], 1000
valid and required to be handled?
â sundar
20 hours ago
@sundar Yes, they should.
â Okx
14 hours ago
The test cases outputs all have whole-minute results, is that intentional? Are inputs like
[10], 5125
or [10, 25, 55], 1000
valid and required to be handled?â sundar
20 hours ago
The test cases outputs all have whole-minute results, is that intentional? Are inputs like
[10], 5125
or [10, 25, 55], 1000
valid and required to be handled?â sundar
20 hours ago
@sundar Yes, they should.
â Okx
14 hours ago
@sundar Yes, they should.
â Okx
14 hours ago
add a comment |Â
13 Answers
13
active
oldest
votes
up vote
6
down vote
R, Â 44Â 43 42 bytes
function(A,D)sum(pmax(0,diff(A)),D*.12)/10
Try it online!
-1 byte by using pmax
as multiple other answers do
Takes inputs as A
scent and D
istance, and returns the time in minutes.
function(A,D) # take Ascent and Distance
diff(A) # take successive differences of ascents
pmax(0, ) # get the positive elements of those
D*.12 # multiply distance by 0.12
sum( , ) # take the sum of all elements
/10 # and divide by 10, returning the result
You can get 4 more bytes using pryr::f(sum(pmax(0,diff(A)),D*.12)/10) instead of using function
â Shayne03
11 hours ago
@Shayne03 that would technically change this answer to "R + pryr" which on the rules of the site counts as a different language than base R, so I'll keep this as it is. Thanks for the suggestion, though!
â Giuseppe
10 hours ago
Thanks, I was unaware.
â Shayne03
10 hours ago
The explanation is shaped like a hill
â John Doe
6 hours ago
add a comment |Â
up vote
3
down vote
JavaScript (ES6), 50 bytes
Saved 1 byte thanks to Giuseppe's answer (dividing by 10 at the end of the process)
Takes input as ([altitudes])(distance)
. Returns the time in minutes.
a=>d=>a.map(p=n=>d-=(x=p-(p=n))<0&&x,d*=.12)&&d/10
Try it online!
add a comment |Â
up vote
2
down vote
05AB1E, 15 bytes
ÃÂ¥ÃÂ0âº}OT÷sâÂÂ;6//+
Try it online!
Returns time in minutes.
Explanation
+ # sum of ...
ÃÂ¥ÃÂ0âº}OT÷ # the sum of positive deltas of the altitude divided by 10
sâÂÂ;6// # the distance divided by 83.33333333 (500/6, or the amount of meters per minute)
Almost exactly what I had in mind. Only difference I had wasâÂÂ12//
instead ofâÂÂ;6//
. So obvious +1 from me.
â Kevin Cruijssen
21 hours ago
add a comment |Â
up vote
2
down vote
Haskell, 47 46 bytes
d#l@(_:t)=d/5e3+sum(max 0<$>zipWith(-)t l)/600
Returns the time in hours.
Try it online!
add a comment |Â
up vote
2
down vote
Python 2, 62 60 bytes
Saved 2 bytes thanks to ovs.
lambda e,d:sum((a-b)*(a>b)for a,b in zip(e[1:],e))*.1+d*.012
Try it online!
Returns time in minutes.
# add all increasing slope differences together
sum(
# multiply the difference by 0 if a<b, else by 1
(a-b)*(a>b)
# create a list of pairs by index, like [(1,0), (2,1) ...(n, n-1)]
# then interate thru the pairs where a is the higher indexed item and b is the lower indexed item
for a,b in zip(e[1:],e)
)
# * 60 minutes / 600 meters == .1 min/m
*.1
# 60 minutes / 5000 meters = .012 min/m
+d*.012
60 bytes
â ovs
yesterday
add a comment |Â
up vote
2
down vote
Perl 6, 45 39 bytes
6 bytes saved thanks to Jo King.
$^l*.012+.1*sum (@^a[1..*]Z-@a)Xmax 0
Try it online!
The first argument is the list with elevations, the second argument is the length of the trek.
Let's start from the middle: @^a[1..*]Z-@a
takes the first argument (saying @^a
, we essentially mean: This is the first argument, and I will be referring to it with @a
from now on. (All the variables with ^
correspond to the arguments in the alphabetical order.)), removes the first element and subtracts the same unmodified list element-by-element from it (stopping when the shorter list runs out). That forms a list of differences. Then we do a "cross product" with a zero using the operator max
, which returns the list of results of the operator max
used on all possible pairs of one element from the list on the left and one on the right. But, since there is only one element on the right, it just acts as a shorthand for using * max 0
on each element of the list. We then sum the results, divide by 10 and add the length $^l$
multiplied by .012, which gives the result in minutes.
1
42 bytes usingX
â Jo King
yesterday
@JoKing, that's a nice use ofX
, thanks!
â Ramillies
yesterday
1
Actually, we can avoid the lastX/
by just dividing the sum by 10. 39 bytes
â Jo King
yesterday
37 bytes using WhateverCode and.&
(returns hours).
â nwellnhof
7 hours ago
add a comment |Â
up vote
1
down vote
Jelly, 12 bytes
ÃÂ.12;I}û0÷âµS
Try it online!
-1 thanks to Mr. Xcoder.
1
Doesn'tÃ.12
work?
â Mr. Xcoder
yesterday
@Mr.Xcoder It does, I was in a hurry.
â Erik the Outgolfer
yesterday
add a comment |Â
up vote
1
down vote
oK, 22 bytes
1_-':x..1.012*
Try it online! Abusing a parsing bug where .1.012
is the same as .1 .012
.
.1.012* /a = [0.1 * input[0], 0.012 * input[1]]
. /function(x=a[0], y=a[1])
1_-':x / a = subtract pairs of elements from x
0| / a = max(a, 0) w/ implicit map
y++/ / y + sum(a)
k, 23 bytes
.1*(.12*y)++/0
Try it online!
1_-':x..1.012*
for 21 bytes? start accumulator withy
.
â streetster
15 hours ago
add a comment |Â
up vote
1
down vote
Pyth, 15 bytes
c+*E.12s>#0.+QT
Full program, expects the set of heights as the first argument, distance as the second. Returns the time in minutes.
Try it online here, or verify all test cases at once here.
c+*E.12s>#0.+QT Implicit: Q=input 1, E=input 2
.+Q Take the differences between each height point
>#0 Filter to remove negative values
s Take the sum
*E.12 Multiply the distance by 0.12
+ Add the two previous results
c T Divide the above by 10, implicit print
add a comment |Â
up vote
1
down vote
APL (Dyalog Unicode), 21 20 18 bytes
.1ÃÂ.12âÂ¥âÂÂ,-+/0âÂÂ2-/âÂÂ
Try it online!
Traditional function taking input (from right to left) as 1st âÂÂ
=Heights/Depths, 2nd âÂÂ
=Distance.
Thanks to @ngn for being a wizard one three bytes.
How it works:
.1ÃÂ.12âÂ¥âÂÂ,-+/0âÂÂ2-/â â Function;
â â Append 0 to the heights vector;
2-/ â Pairwise (2) differences (-/);
0â â Minimum between 0 and the vector's elements;
+/ â Sum (yields the negated total height);
- â Subtract from;
.12âÂ¥âÂÂ, â Distance à0.12;
.1àâ And divide by 10;
thanks for "wizard" :) you don't have to copy the expression multiple times in order to test it, put it in a tradfn instead;,0
is unnecessary, theâµ
for the problematic test should be,604
, not604
â ngn
20 hours ago
See, that's why you're a wizard. The copying the expression multiple times part is totally my fault, I just replaced theâº
andâµ
in the old code withâÂÂ
and was too lazy to put the tradfn header/footer. The,0
bit though? Gold.
â J. Sallé
15 hours ago
add a comment |Â
up vote
0
down vote
Python 2, 59 bytes
lambda a,d:d/5e3+sum(max(0,y-x)/6e2for x,y in zip(a,a[1:]))
Try it online!
returns hours as a decimal.
add a comment |Â
up vote
0
down vote
Java 8, 89 bytes
a->n->int s=0,p=0,i=a.length;for(;i-->0;p=a[i])s+=(p=p-a[i])>0?p:0;return s/10+n/500*6;
Try it online.
Explanation:
a->n-> // Method with integer-array and integer parameter and integer return-type
int s=0, // Sum-integers, starting at 0
p=0, // Previous integer, starting at 0
i=a.length;for(;i-->0; // Loop `i` backwards over the array
; // After every iteration:
p=a[i]) // Set `p` to the current value for the next iteration
s+=(p=p-a[i])>0? // If the previous minus current item is larger than 0:
p // Add that difference to the sum `s`
: // Else:
0; // Leave the sum the same
return s/10 // Return the sum integer-divided by 10
+n/500*6; // Plus the second input divided by 500 and then multiplied by 6
add a comment |Â
up vote
0
down vote
Japt, 39 bytes
0oUl)x@W=UgXà-UgX)g ÃÂ¥ÃÂ?0:W/#ÃÂ} +V/(#ô0
Try it online!
Likely can be golfed quite a bit more.
add a comment |Â
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
R, Â 44Â 43 42 bytes
function(A,D)sum(pmax(0,diff(A)),D*.12)/10
Try it online!
-1 byte by using pmax
as multiple other answers do
Takes inputs as A
scent and D
istance, and returns the time in minutes.
function(A,D) # take Ascent and Distance
diff(A) # take successive differences of ascents
pmax(0, ) # get the positive elements of those
D*.12 # multiply distance by 0.12
sum( , ) # take the sum of all elements
/10 # and divide by 10, returning the result
You can get 4 more bytes using pryr::f(sum(pmax(0,diff(A)),D*.12)/10) instead of using function
â Shayne03
11 hours ago
@Shayne03 that would technically change this answer to "R + pryr" which on the rules of the site counts as a different language than base R, so I'll keep this as it is. Thanks for the suggestion, though!
â Giuseppe
10 hours ago
Thanks, I was unaware.
â Shayne03
10 hours ago
The explanation is shaped like a hill
â John Doe
6 hours ago
add a comment |Â
up vote
6
down vote
R, Â 44Â 43 42 bytes
function(A,D)sum(pmax(0,diff(A)),D*.12)/10
Try it online!
-1 byte by using pmax
as multiple other answers do
Takes inputs as A
scent and D
istance, and returns the time in minutes.
function(A,D) # take Ascent and Distance
diff(A) # take successive differences of ascents
pmax(0, ) # get the positive elements of those
D*.12 # multiply distance by 0.12
sum( , ) # take the sum of all elements
/10 # and divide by 10, returning the result
You can get 4 more bytes using pryr::f(sum(pmax(0,diff(A)),D*.12)/10) instead of using function
â Shayne03
11 hours ago
@Shayne03 that would technically change this answer to "R + pryr" which on the rules of the site counts as a different language than base R, so I'll keep this as it is. Thanks for the suggestion, though!
â Giuseppe
10 hours ago
Thanks, I was unaware.
â Shayne03
10 hours ago
The explanation is shaped like a hill
â John Doe
6 hours ago
add a comment |Â
up vote
6
down vote
up vote
6
down vote
R, Â 44Â 43 42 bytes
function(A,D)sum(pmax(0,diff(A)),D*.12)/10
Try it online!
-1 byte by using pmax
as multiple other answers do
Takes inputs as A
scent and D
istance, and returns the time in minutes.
function(A,D) # take Ascent and Distance
diff(A) # take successive differences of ascents
pmax(0, ) # get the positive elements of those
D*.12 # multiply distance by 0.12
sum( , ) # take the sum of all elements
/10 # and divide by 10, returning the result
R, Â 44Â 43 42 bytes
function(A,D)sum(pmax(0,diff(A)),D*.12)/10
Try it online!
-1 byte by using pmax
as multiple other answers do
Takes inputs as A
scent and D
istance, and returns the time in minutes.
function(A,D) # take Ascent and Distance
diff(A) # take successive differences of ascents
pmax(0, ) # get the positive elements of those
D*.12 # multiply distance by 0.12
sum( , ) # take the sum of all elements
/10 # and divide by 10, returning the result
edited yesterday
answered yesterday
Giuseppe
13.8k3946
13.8k3946
You can get 4 more bytes using pryr::f(sum(pmax(0,diff(A)),D*.12)/10) instead of using function
â Shayne03
11 hours ago
@Shayne03 that would technically change this answer to "R + pryr" which on the rules of the site counts as a different language than base R, so I'll keep this as it is. Thanks for the suggestion, though!
â Giuseppe
10 hours ago
Thanks, I was unaware.
â Shayne03
10 hours ago
The explanation is shaped like a hill
â John Doe
6 hours ago
add a comment |Â
You can get 4 more bytes using pryr::f(sum(pmax(0,diff(A)),D*.12)/10) instead of using function
â Shayne03
11 hours ago
@Shayne03 that would technically change this answer to "R + pryr" which on the rules of the site counts as a different language than base R, so I'll keep this as it is. Thanks for the suggestion, though!
â Giuseppe
10 hours ago
Thanks, I was unaware.
â Shayne03
10 hours ago
The explanation is shaped like a hill
â John Doe
6 hours ago
You can get 4 more bytes using pryr::f(sum(pmax(0,diff(A)),D*.12)/10) instead of using function
â Shayne03
11 hours ago
You can get 4 more bytes using pryr::f(sum(pmax(0,diff(A)),D*.12)/10) instead of using function
â Shayne03
11 hours ago
@Shayne03 that would technically change this answer to "R + pryr" which on the rules of the site counts as a different language than base R, so I'll keep this as it is. Thanks for the suggestion, though!
â Giuseppe
10 hours ago
@Shayne03 that would technically change this answer to "R + pryr" which on the rules of the site counts as a different language than base R, so I'll keep this as it is. Thanks for the suggestion, though!
â Giuseppe
10 hours ago
Thanks, I was unaware.
â Shayne03
10 hours ago
Thanks, I was unaware.
â Shayne03
10 hours ago
The explanation is shaped like a hill
â John Doe
6 hours ago
The explanation is shaped like a hill
â John Doe
6 hours ago
add a comment |Â
up vote
3
down vote
JavaScript (ES6), 50 bytes
Saved 1 byte thanks to Giuseppe's answer (dividing by 10 at the end of the process)
Takes input as ([altitudes])(distance)
. Returns the time in minutes.
a=>d=>a.map(p=n=>d-=(x=p-(p=n))<0&&x,d*=.12)&&d/10
Try it online!
add a comment |Â
up vote
3
down vote
JavaScript (ES6), 50 bytes
Saved 1 byte thanks to Giuseppe's answer (dividing by 10 at the end of the process)
Takes input as ([altitudes])(distance)
. Returns the time in minutes.
a=>d=>a.map(p=n=>d-=(x=p-(p=n))<0&&x,d*=.12)&&d/10
Try it online!
add a comment |Â
up vote
3
down vote
up vote
3
down vote
JavaScript (ES6), 50 bytes
Saved 1 byte thanks to Giuseppe's answer (dividing by 10 at the end of the process)
Takes input as ([altitudes])(distance)
. Returns the time in minutes.
a=>d=>a.map(p=n=>d-=(x=p-(p=n))<0&&x,d*=.12)&&d/10
Try it online!
JavaScript (ES6), 50 bytes
Saved 1 byte thanks to Giuseppe's answer (dividing by 10 at the end of the process)
Takes input as ([altitudes])(distance)
. Returns the time in minutes.
a=>d=>a.map(p=n=>d-=(x=p-(p=n))<0&&x,d*=.12)&&d/10
Try it online!
edited yesterday
answered yesterday
Arnauld
60.4k472253
60.4k472253
add a comment |Â
add a comment |Â
up vote
2
down vote
05AB1E, 15 bytes
ÃÂ¥ÃÂ0âº}OT÷sâÂÂ;6//+
Try it online!
Returns time in minutes.
Explanation
+ # sum of ...
ÃÂ¥ÃÂ0âº}OT÷ # the sum of positive deltas of the altitude divided by 10
sâÂÂ;6// # the distance divided by 83.33333333 (500/6, or the amount of meters per minute)
Almost exactly what I had in mind. Only difference I had wasâÂÂ12//
instead ofâÂÂ;6//
. So obvious +1 from me.
â Kevin Cruijssen
21 hours ago
add a comment |Â
up vote
2
down vote
05AB1E, 15 bytes
ÃÂ¥ÃÂ0âº}OT÷sâÂÂ;6//+
Try it online!
Returns time in minutes.
Explanation
+ # sum of ...
ÃÂ¥ÃÂ0âº}OT÷ # the sum of positive deltas of the altitude divided by 10
sâÂÂ;6// # the distance divided by 83.33333333 (500/6, or the amount of meters per minute)
Almost exactly what I had in mind. Only difference I had wasâÂÂ12//
instead ofâÂÂ;6//
. So obvious +1 from me.
â Kevin Cruijssen
21 hours ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
05AB1E, 15 bytes
ÃÂ¥ÃÂ0âº}OT÷sâÂÂ;6//+
Try it online!
Returns time in minutes.
Explanation
+ # sum of ...
ÃÂ¥ÃÂ0âº}OT÷ # the sum of positive deltas of the altitude divided by 10
sâÂÂ;6// # the distance divided by 83.33333333 (500/6, or the amount of meters per minute)
05AB1E, 15 bytes
ÃÂ¥ÃÂ0âº}OT÷sâÂÂ;6//+
Try it online!
Returns time in minutes.
Explanation
+ # sum of ...
ÃÂ¥ÃÂ0âº}OT÷ # the sum of positive deltas of the altitude divided by 10
sâÂÂ;6// # the distance divided by 83.33333333 (500/6, or the amount of meters per minute)
answered yesterday
Kaldo
9149
9149
Almost exactly what I had in mind. Only difference I had wasâÂÂ12//
instead ofâÂÂ;6//
. So obvious +1 from me.
â Kevin Cruijssen
21 hours ago
add a comment |Â
Almost exactly what I had in mind. Only difference I had wasâÂÂ12//
instead ofâÂÂ;6//
. So obvious +1 from me.
â Kevin Cruijssen
21 hours ago
Almost exactly what I had in mind. Only difference I had was
âÂÂ12//
instead of âÂÂ;6//
. So obvious +1 from me.â Kevin Cruijssen
21 hours ago
Almost exactly what I had in mind. Only difference I had was
âÂÂ12//
instead of âÂÂ;6//
. So obvious +1 from me.â Kevin Cruijssen
21 hours ago
add a comment |Â
up vote
2
down vote
Haskell, 47 46 bytes
d#l@(_:t)=d/5e3+sum(max 0<$>zipWith(-)t l)/600
Returns the time in hours.
Try it online!
add a comment |Â
up vote
2
down vote
Haskell, 47 46 bytes
d#l@(_:t)=d/5e3+sum(max 0<$>zipWith(-)t l)/600
Returns the time in hours.
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Haskell, 47 46 bytes
d#l@(_:t)=d/5e3+sum(max 0<$>zipWith(-)t l)/600
Returns the time in hours.
Try it online!
Haskell, 47 46 bytes
d#l@(_:t)=d/5e3+sum(max 0<$>zipWith(-)t l)/600
Returns the time in hours.
Try it online!
edited yesterday
answered yesterday
nimi
29.6k31880
29.6k31880
add a comment |Â
add a comment |Â
up vote
2
down vote
Python 2, 62 60 bytes
Saved 2 bytes thanks to ovs.
lambda e,d:sum((a-b)*(a>b)for a,b in zip(e[1:],e))*.1+d*.012
Try it online!
Returns time in minutes.
# add all increasing slope differences together
sum(
# multiply the difference by 0 if a<b, else by 1
(a-b)*(a>b)
# create a list of pairs by index, like [(1,0), (2,1) ...(n, n-1)]
# then interate thru the pairs where a is the higher indexed item and b is the lower indexed item
for a,b in zip(e[1:],e)
)
# * 60 minutes / 600 meters == .1 min/m
*.1
# 60 minutes / 5000 meters = .012 min/m
+d*.012
60 bytes
â ovs
yesterday
add a comment |Â
up vote
2
down vote
Python 2, 62 60 bytes
Saved 2 bytes thanks to ovs.
lambda e,d:sum((a-b)*(a>b)for a,b in zip(e[1:],e))*.1+d*.012
Try it online!
Returns time in minutes.
# add all increasing slope differences together
sum(
# multiply the difference by 0 if a<b, else by 1
(a-b)*(a>b)
# create a list of pairs by index, like [(1,0), (2,1) ...(n, n-1)]
# then interate thru the pairs where a is the higher indexed item and b is the lower indexed item
for a,b in zip(e[1:],e)
)
# * 60 minutes / 600 meters == .1 min/m
*.1
# 60 minutes / 5000 meters = .012 min/m
+d*.012
60 bytes
â ovs
yesterday
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Python 2, 62 60 bytes
Saved 2 bytes thanks to ovs.
lambda e,d:sum((a-b)*(a>b)for a,b in zip(e[1:],e))*.1+d*.012
Try it online!
Returns time in minutes.
# add all increasing slope differences together
sum(
# multiply the difference by 0 if a<b, else by 1
(a-b)*(a>b)
# create a list of pairs by index, like [(1,0), (2,1) ...(n, n-1)]
# then interate thru the pairs where a is the higher indexed item and b is the lower indexed item
for a,b in zip(e[1:],e)
)
# * 60 minutes / 600 meters == .1 min/m
*.1
# 60 minutes / 5000 meters = .012 min/m
+d*.012
Python 2, 62 60 bytes
Saved 2 bytes thanks to ovs.
lambda e,d:sum((a-b)*(a>b)for a,b in zip(e[1:],e))*.1+d*.012
Try it online!
Returns time in minutes.
# add all increasing slope differences together
sum(
# multiply the difference by 0 if a<b, else by 1
(a-b)*(a>b)
# create a list of pairs by index, like [(1,0), (2,1) ...(n, n-1)]
# then interate thru the pairs where a is the higher indexed item and b is the lower indexed item
for a,b in zip(e[1:],e)
)
# * 60 minutes / 600 meters == .1 min/m
*.1
# 60 minutes / 5000 meters = .012 min/m
+d*.012
edited yesterday
answered yesterday
Triggernometry
934
934
60 bytes
â ovs
yesterday
add a comment |Â
60 bytes
â ovs
yesterday
60 bytes
â ovs
yesterday
60 bytes
â ovs
yesterday
add a comment |Â
up vote
2
down vote
Perl 6, 45 39 bytes
6 bytes saved thanks to Jo King.
$^l*.012+.1*sum (@^a[1..*]Z-@a)Xmax 0
Try it online!
The first argument is the list with elevations, the second argument is the length of the trek.
Let's start from the middle: @^a[1..*]Z-@a
takes the first argument (saying @^a
, we essentially mean: This is the first argument, and I will be referring to it with @a
from now on. (All the variables with ^
correspond to the arguments in the alphabetical order.)), removes the first element and subtracts the same unmodified list element-by-element from it (stopping when the shorter list runs out). That forms a list of differences. Then we do a "cross product" with a zero using the operator max
, which returns the list of results of the operator max
used on all possible pairs of one element from the list on the left and one on the right. But, since there is only one element on the right, it just acts as a shorthand for using * max 0
on each element of the list. We then sum the results, divide by 10 and add the length $^l$
multiplied by .012, which gives the result in minutes.
1
42 bytes usingX
â Jo King
yesterday
@JoKing, that's a nice use ofX
, thanks!
â Ramillies
yesterday
1
Actually, we can avoid the lastX/
by just dividing the sum by 10. 39 bytes
â Jo King
yesterday
37 bytes using WhateverCode and.&
(returns hours).
â nwellnhof
7 hours ago
add a comment |Â
up vote
2
down vote
Perl 6, 45 39 bytes
6 bytes saved thanks to Jo King.
$^l*.012+.1*sum (@^a[1..*]Z-@a)Xmax 0
Try it online!
The first argument is the list with elevations, the second argument is the length of the trek.
Let's start from the middle: @^a[1..*]Z-@a
takes the first argument (saying @^a
, we essentially mean: This is the first argument, and I will be referring to it with @a
from now on. (All the variables with ^
correspond to the arguments in the alphabetical order.)), removes the first element and subtracts the same unmodified list element-by-element from it (stopping when the shorter list runs out). That forms a list of differences. Then we do a "cross product" with a zero using the operator max
, which returns the list of results of the operator max
used on all possible pairs of one element from the list on the left and one on the right. But, since there is only one element on the right, it just acts as a shorthand for using * max 0
on each element of the list. We then sum the results, divide by 10 and add the length $^l$
multiplied by .012, which gives the result in minutes.
1
42 bytes usingX
â Jo King
yesterday
@JoKing, that's a nice use ofX
, thanks!
â Ramillies
yesterday
1
Actually, we can avoid the lastX/
by just dividing the sum by 10. 39 bytes
â Jo King
yesterday
37 bytes using WhateverCode and.&
(returns hours).
â nwellnhof
7 hours ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Perl 6, 45 39 bytes
6 bytes saved thanks to Jo King.
$^l*.012+.1*sum (@^a[1..*]Z-@a)Xmax 0
Try it online!
The first argument is the list with elevations, the second argument is the length of the trek.
Let's start from the middle: @^a[1..*]Z-@a
takes the first argument (saying @^a
, we essentially mean: This is the first argument, and I will be referring to it with @a
from now on. (All the variables with ^
correspond to the arguments in the alphabetical order.)), removes the first element and subtracts the same unmodified list element-by-element from it (stopping when the shorter list runs out). That forms a list of differences. Then we do a "cross product" with a zero using the operator max
, which returns the list of results of the operator max
used on all possible pairs of one element from the list on the left and one on the right. But, since there is only one element on the right, it just acts as a shorthand for using * max 0
on each element of the list. We then sum the results, divide by 10 and add the length $^l$
multiplied by .012, which gives the result in minutes.
Perl 6, 45 39 bytes
6 bytes saved thanks to Jo King.
$^l*.012+.1*sum (@^a[1..*]Z-@a)Xmax 0
Try it online!
The first argument is the list with elevations, the second argument is the length of the trek.
Let's start from the middle: @^a[1..*]Z-@a
takes the first argument (saying @^a
, we essentially mean: This is the first argument, and I will be referring to it with @a
from now on. (All the variables with ^
correspond to the arguments in the alphabetical order.)), removes the first element and subtracts the same unmodified list element-by-element from it (stopping when the shorter list runs out). That forms a list of differences. Then we do a "cross product" with a zero using the operator max
, which returns the list of results of the operator max
used on all possible pairs of one element from the list on the left and one on the right. But, since there is only one element on the right, it just acts as a shorthand for using * max 0
on each element of the list. We then sum the results, divide by 10 and add the length $^l$
multiplied by .012, which gives the result in minutes.
edited 21 hours ago
answered yesterday
Ramillies
1,321411
1,321411
1
42 bytes usingX
â Jo King
yesterday
@JoKing, that's a nice use ofX
, thanks!
â Ramillies
yesterday
1
Actually, we can avoid the lastX/
by just dividing the sum by 10. 39 bytes
â Jo King
yesterday
37 bytes using WhateverCode and.&
(returns hours).
â nwellnhof
7 hours ago
add a comment |Â
1
42 bytes usingX
â Jo King
yesterday
@JoKing, that's a nice use ofX
, thanks!
â Ramillies
yesterday
1
Actually, we can avoid the lastX/
by just dividing the sum by 10. 39 bytes
â Jo King
yesterday
37 bytes using WhateverCode and.&
(returns hours).
â nwellnhof
7 hours ago
1
1
42 bytes using
X
â Jo King
yesterday
42 bytes using
X
â Jo King
yesterday
@JoKing, that's a nice use of
X
, thanks!â Ramillies
yesterday
@JoKing, that's a nice use of
X
, thanks!â Ramillies
yesterday
1
1
Actually, we can avoid the last
X/
by just dividing the sum by 10. 39 bytesâ Jo King
yesterday
Actually, we can avoid the last
X/
by just dividing the sum by 10. 39 bytesâ Jo King
yesterday
37 bytes using WhateverCode and
.&
(returns hours).â nwellnhof
7 hours ago
37 bytes using WhateverCode and
.&
(returns hours).â nwellnhof
7 hours ago
add a comment |Â
up vote
1
down vote
Jelly, 12 bytes
ÃÂ.12;I}û0÷âµS
Try it online!
-1 thanks to Mr. Xcoder.
1
Doesn'tÃ.12
work?
â Mr. Xcoder
yesterday
@Mr.Xcoder It does, I was in a hurry.
â Erik the Outgolfer
yesterday
add a comment |Â
up vote
1
down vote
Jelly, 12 bytes
ÃÂ.12;I}û0÷âµS
Try it online!
-1 thanks to Mr. Xcoder.
1
Doesn'tÃ.12
work?
â Mr. Xcoder
yesterday
@Mr.Xcoder It does, I was in a hurry.
â Erik the Outgolfer
yesterday
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Jelly, 12 bytes
ÃÂ.12;I}û0÷âµS
Try it online!
-1 thanks to Mr. Xcoder.
Jelly, 12 bytes
ÃÂ.12;I}û0÷âµS
Try it online!
-1 thanks to Mr. Xcoder.
edited yesterday
answered yesterday
Erik the Outgolfer
29.1k42697
29.1k42697
1
Doesn'tÃ.12
work?
â Mr. Xcoder
yesterday
@Mr.Xcoder It does, I was in a hurry.
â Erik the Outgolfer
yesterday
add a comment |Â
1
Doesn'tÃ.12
work?
â Mr. Xcoder
yesterday
@Mr.Xcoder It does, I was in a hurry.
â Erik the Outgolfer
yesterday
1
1
Doesn't
Ã.12
work?â Mr. Xcoder
yesterday
Doesn't
Ã.12
work?â Mr. Xcoder
yesterday
@Mr.Xcoder It does, I was in a hurry.
â Erik the Outgolfer
yesterday
@Mr.Xcoder It does, I was in a hurry.
â Erik the Outgolfer
yesterday
add a comment |Â
up vote
1
down vote
oK, 22 bytes
1_-':x..1.012*
Try it online! Abusing a parsing bug where .1.012
is the same as .1 .012
.
.1.012* /a = [0.1 * input[0], 0.012 * input[1]]
. /function(x=a[0], y=a[1])
1_-':x / a = subtract pairs of elements from x
0| / a = max(a, 0) w/ implicit map
y++/ / y + sum(a)
k, 23 bytes
.1*(.12*y)++/0
Try it online!
1_-':x..1.012*
for 21 bytes? start accumulator withy
.
â streetster
15 hours ago
add a comment |Â
up vote
1
down vote
oK, 22 bytes
1_-':x..1.012*
Try it online! Abusing a parsing bug where .1.012
is the same as .1 .012
.
.1.012* /a = [0.1 * input[0], 0.012 * input[1]]
. /function(x=a[0], y=a[1])
1_-':x / a = subtract pairs of elements from x
0| / a = max(a, 0) w/ implicit map
y++/ / y + sum(a)
k, 23 bytes
.1*(.12*y)++/0
Try it online!
1_-':x..1.012*
for 21 bytes? start accumulator withy
.
â streetster
15 hours ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
oK, 22 bytes
1_-':x..1.012*
Try it online! Abusing a parsing bug where .1.012
is the same as .1 .012
.
.1.012* /a = [0.1 * input[0], 0.012 * input[1]]
. /function(x=a[0], y=a[1])
1_-':x / a = subtract pairs of elements from x
0| / a = max(a, 0) w/ implicit map
y++/ / y + sum(a)
k, 23 bytes
.1*(.12*y)++/0
Try it online!
oK, 22 bytes
1_-':x..1.012*
Try it online! Abusing a parsing bug where .1.012
is the same as .1 .012
.
.1.012* /a = [0.1 * input[0], 0.012 * input[1]]
. /function(x=a[0], y=a[1])
1_-':x / a = subtract pairs of elements from x
0| / a = max(a, 0) w/ implicit map
y++/ / y + sum(a)
k, 23 bytes
.1*(.12*y)++/0
Try it online!
answered yesterday
zgrep
1,12138
1,12138
1_-':x..1.012*
for 21 bytes? start accumulator withy
.
â streetster
15 hours ago
add a comment |Â
1_-':x..1.012*
for 21 bytes? start accumulator withy
.
â streetster
15 hours ago
1_-':x..1.012*
for 21 bytes? start accumulator with y
.â streetster
15 hours ago
1_-':x..1.012*
for 21 bytes? start accumulator with y
.â streetster
15 hours ago
add a comment |Â
up vote
1
down vote
Pyth, 15 bytes
c+*E.12s>#0.+QT
Full program, expects the set of heights as the first argument, distance as the second. Returns the time in minutes.
Try it online here, or verify all test cases at once here.
c+*E.12s>#0.+QT Implicit: Q=input 1, E=input 2
.+Q Take the differences between each height point
>#0 Filter to remove negative values
s Take the sum
*E.12 Multiply the distance by 0.12
+ Add the two previous results
c T Divide the above by 10, implicit print
add a comment |Â
up vote
1
down vote
Pyth, 15 bytes
c+*E.12s>#0.+QT
Full program, expects the set of heights as the first argument, distance as the second. Returns the time in minutes.
Try it online here, or verify all test cases at once here.
c+*E.12s>#0.+QT Implicit: Q=input 1, E=input 2
.+Q Take the differences between each height point
>#0 Filter to remove negative values
s Take the sum
*E.12 Multiply the distance by 0.12
+ Add the two previous results
c T Divide the above by 10, implicit print
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Pyth, 15 bytes
c+*E.12s>#0.+QT
Full program, expects the set of heights as the first argument, distance as the second. Returns the time in minutes.
Try it online here, or verify all test cases at once here.
c+*E.12s>#0.+QT Implicit: Q=input 1, E=input 2
.+Q Take the differences between each height point
>#0 Filter to remove negative values
s Take the sum
*E.12 Multiply the distance by 0.12
+ Add the two previous results
c T Divide the above by 10, implicit print
Pyth, 15 bytes
c+*E.12s>#0.+QT
Full program, expects the set of heights as the first argument, distance as the second. Returns the time in minutes.
Try it online here, or verify all test cases at once here.
c+*E.12s>#0.+QT Implicit: Q=input 1, E=input 2
.+Q Take the differences between each height point
>#0 Filter to remove negative values
s Take the sum
*E.12 Multiply the distance by 0.12
+ Add the two previous results
c T Divide the above by 10, implicit print
answered 19 hours ago
Sok
2,821622
2,821622
add a comment |Â
add a comment |Â
up vote
1
down vote
APL (Dyalog Unicode), 21 20 18 bytes
.1ÃÂ.12âÂ¥âÂÂ,-+/0âÂÂ2-/âÂÂ
Try it online!
Traditional function taking input (from right to left) as 1st âÂÂ
=Heights/Depths, 2nd âÂÂ
=Distance.
Thanks to @ngn for being a wizard one three bytes.
How it works:
.1ÃÂ.12âÂ¥âÂÂ,-+/0âÂÂ2-/â â Function;
â â Append 0 to the heights vector;
2-/ â Pairwise (2) differences (-/);
0â â Minimum between 0 and the vector's elements;
+/ â Sum (yields the negated total height);
- â Subtract from;
.12âÂ¥âÂÂ, â Distance à0.12;
.1àâ And divide by 10;
thanks for "wizard" :) you don't have to copy the expression multiple times in order to test it, put it in a tradfn instead;,0
is unnecessary, theâµ
for the problematic test should be,604
, not604
â ngn
20 hours ago
See, that's why you're a wizard. The copying the expression multiple times part is totally my fault, I just replaced theâº
andâµ
in the old code withâÂÂ
and was too lazy to put the tradfn header/footer. The,0
bit though? Gold.
â J. Sallé
15 hours ago
add a comment |Â
up vote
1
down vote
APL (Dyalog Unicode), 21 20 18 bytes
.1ÃÂ.12âÂ¥âÂÂ,-+/0âÂÂ2-/âÂÂ
Try it online!
Traditional function taking input (from right to left) as 1st âÂÂ
=Heights/Depths, 2nd âÂÂ
=Distance.
Thanks to @ngn for being a wizard one three bytes.
How it works:
.1ÃÂ.12âÂ¥âÂÂ,-+/0âÂÂ2-/â â Function;
â â Append 0 to the heights vector;
2-/ â Pairwise (2) differences (-/);
0â â Minimum between 0 and the vector's elements;
+/ â Sum (yields the negated total height);
- â Subtract from;
.12âÂ¥âÂÂ, â Distance à0.12;
.1àâ And divide by 10;
thanks for "wizard" :) you don't have to copy the expression multiple times in order to test it, put it in a tradfn instead;,0
is unnecessary, theâµ
for the problematic test should be,604
, not604
â ngn
20 hours ago
See, that's why you're a wizard. The copying the expression multiple times part is totally my fault, I just replaced theâº
andâµ
in the old code withâÂÂ
and was too lazy to put the tradfn header/footer. The,0
bit though? Gold.
â J. Sallé
15 hours ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
APL (Dyalog Unicode), 21 20 18 bytes
.1ÃÂ.12âÂ¥âÂÂ,-+/0âÂÂ2-/âÂÂ
Try it online!
Traditional function taking input (from right to left) as 1st âÂÂ
=Heights/Depths, 2nd âÂÂ
=Distance.
Thanks to @ngn for being a wizard one three bytes.
How it works:
.1ÃÂ.12âÂ¥âÂÂ,-+/0âÂÂ2-/â â Function;
â â Append 0 to the heights vector;
2-/ â Pairwise (2) differences (-/);
0â â Minimum between 0 and the vector's elements;
+/ â Sum (yields the negated total height);
- â Subtract from;
.12âÂ¥âÂÂ, â Distance à0.12;
.1àâ And divide by 10;
APL (Dyalog Unicode), 21 20 18 bytes
.1ÃÂ.12âÂ¥âÂÂ,-+/0âÂÂ2-/âÂÂ
Try it online!
Traditional function taking input (from right to left) as 1st âÂÂ
=Heights/Depths, 2nd âÂÂ
=Distance.
Thanks to @ngn for being a wizard one three bytes.
How it works:
.1ÃÂ.12âÂ¥âÂÂ,-+/0âÂÂ2-/â â Function;
â â Append 0 to the heights vector;
2-/ â Pairwise (2) differences (-/);
0â â Minimum between 0 and the vector's elements;
+/ â Sum (yields the negated total height);
- â Subtract from;
.12âÂ¥âÂÂ, â Distance à0.12;
.1àâ And divide by 10;
edited 15 hours ago
answered yesterday
J. Sallé
1,288218
1,288218
thanks for "wizard" :) you don't have to copy the expression multiple times in order to test it, put it in a tradfn instead;,0
is unnecessary, theâµ
for the problematic test should be,604
, not604
â ngn
20 hours ago
See, that's why you're a wizard. The copying the expression multiple times part is totally my fault, I just replaced theâº
andâµ
in the old code withâÂÂ
and was too lazy to put the tradfn header/footer. The,0
bit though? Gold.
â J. Sallé
15 hours ago
add a comment |Â
thanks for "wizard" :) you don't have to copy the expression multiple times in order to test it, put it in a tradfn instead;,0
is unnecessary, theâµ
for the problematic test should be,604
, not604
â ngn
20 hours ago
See, that's why you're a wizard. The copying the expression multiple times part is totally my fault, I just replaced theâº
andâµ
in the old code withâÂÂ
and was too lazy to put the tradfn header/footer. The,0
bit though? Gold.
â J. Sallé
15 hours ago
thanks for "wizard" :) you don't have to copy the expression multiple times in order to test it, put it in a tradfn instead;
,0
is unnecessary, the âµ
for the problematic test should be ,604
, not 604
â ngn
20 hours ago
thanks for "wizard" :) you don't have to copy the expression multiple times in order to test it, put it in a tradfn instead;
,0
is unnecessary, the âµ
for the problematic test should be ,604
, not 604
â ngn
20 hours ago
See, that's why you're a wizard. The copying the expression multiple times part is totally my fault, I just replaced the
âº
and âµ
in the old code with âÂÂ
and was too lazy to put the tradfn header/footer. The ,0
bit though? Gold.â J. Sallé
15 hours ago
See, that's why you're a wizard. The copying the expression multiple times part is totally my fault, I just replaced the
âº
and âµ
in the old code with âÂÂ
and was too lazy to put the tradfn header/footer. The ,0
bit though? Gold.â J. Sallé
15 hours ago
add a comment |Â
up vote
0
down vote
Python 2, 59 bytes
lambda a,d:d/5e3+sum(max(0,y-x)/6e2for x,y in zip(a,a[1:]))
Try it online!
returns hours as a decimal.
add a comment |Â
up vote
0
down vote
Python 2, 59 bytes
lambda a,d:d/5e3+sum(max(0,y-x)/6e2for x,y in zip(a,a[1:]))
Try it online!
returns hours as a decimal.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Python 2, 59 bytes
lambda a,d:d/5e3+sum(max(0,y-x)/6e2for x,y in zip(a,a[1:]))
Try it online!
returns hours as a decimal.
Python 2, 59 bytes
lambda a,d:d/5e3+sum(max(0,y-x)/6e2for x,y in zip(a,a[1:]))
Try it online!
returns hours as a decimal.
answered yesterday
Chas Brown
3,570216
3,570216
add a comment |Â
add a comment |Â
up vote
0
down vote
Java 8, 89 bytes
a->n->int s=0,p=0,i=a.length;for(;i-->0;p=a[i])s+=(p=p-a[i])>0?p:0;return s/10+n/500*6;
Try it online.
Explanation:
a->n-> // Method with integer-array and integer parameter and integer return-type
int s=0, // Sum-integers, starting at 0
p=0, // Previous integer, starting at 0
i=a.length;for(;i-->0; // Loop `i` backwards over the array
; // After every iteration:
p=a[i]) // Set `p` to the current value for the next iteration
s+=(p=p-a[i])>0? // If the previous minus current item is larger than 0:
p // Add that difference to the sum `s`
: // Else:
0; // Leave the sum the same
return s/10 // Return the sum integer-divided by 10
+n/500*6; // Plus the second input divided by 500 and then multiplied by 6
add a comment |Â
up vote
0
down vote
Java 8, 89 bytes
a->n->int s=0,p=0,i=a.length;for(;i-->0;p=a[i])s+=(p=p-a[i])>0?p:0;return s/10+n/500*6;
Try it online.
Explanation:
a->n-> // Method with integer-array and integer parameter and integer return-type
int s=0, // Sum-integers, starting at 0
p=0, // Previous integer, starting at 0
i=a.length;for(;i-->0; // Loop `i` backwards over the array
; // After every iteration:
p=a[i]) // Set `p` to the current value for the next iteration
s+=(p=p-a[i])>0? // If the previous minus current item is larger than 0:
p // Add that difference to the sum `s`
: // Else:
0; // Leave the sum the same
return s/10 // Return the sum integer-divided by 10
+n/500*6; // Plus the second input divided by 500 and then multiplied by 6
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Java 8, 89 bytes
a->n->int s=0,p=0,i=a.length;for(;i-->0;p=a[i])s+=(p=p-a[i])>0?p:0;return s/10+n/500*6;
Try it online.
Explanation:
a->n-> // Method with integer-array and integer parameter and integer return-type
int s=0, // Sum-integers, starting at 0
p=0, // Previous integer, starting at 0
i=a.length;for(;i-->0; // Loop `i` backwards over the array
; // After every iteration:
p=a[i]) // Set `p` to the current value for the next iteration
s+=(p=p-a[i])>0? // If the previous minus current item is larger than 0:
p // Add that difference to the sum `s`
: // Else:
0; // Leave the sum the same
return s/10 // Return the sum integer-divided by 10
+n/500*6; // Plus the second input divided by 500 and then multiplied by 6
Java 8, 89 bytes
a->n->int s=0,p=0,i=a.length;for(;i-->0;p=a[i])s+=(p=p-a[i])>0?p:0;return s/10+n/500*6;
Try it online.
Explanation:
a->n-> // Method with integer-array and integer parameter and integer return-type
int s=0, // Sum-integers, starting at 0
p=0, // Previous integer, starting at 0
i=a.length;for(;i-->0; // Loop `i` backwards over the array
; // After every iteration:
p=a[i]) // Set `p` to the current value for the next iteration
s+=(p=p-a[i])>0? // If the previous minus current item is larger than 0:
p // Add that difference to the sum `s`
: // Else:
0; // Leave the sum the same
return s/10 // Return the sum integer-divided by 10
+n/500*6; // Plus the second input divided by 500 and then multiplied by 6
answered 21 hours ago
Kevin Cruijssen
27.4k545151
27.4k545151
add a comment |Â
add a comment |Â
up vote
0
down vote
Japt, 39 bytes
0oUl)x@W=UgXà-UgX)g ÃÂ¥ÃÂ?0:W/#ÃÂ} +V/(#ô0
Try it online!
Likely can be golfed quite a bit more.
add a comment |Â
up vote
0
down vote
Japt, 39 bytes
0oUl)x@W=UgXà-UgX)g ÃÂ¥ÃÂ?0:W/#ÃÂ} +V/(#ô0
Try it online!
Likely can be golfed quite a bit more.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Japt, 39 bytes
0oUl)x@W=UgXà-UgX)g ÃÂ¥ÃÂ?0:W/#ÃÂ} +V/(#ô0
Try it online!
Likely can be golfed quite a bit more.
Japt, 39 bytes
0oUl)x@W=UgXà-UgX)g ÃÂ¥ÃÂ?0:W/#ÃÂ} +V/(#ô0
Try it online!
Likely can be golfed quite a bit more.
edited 14 hours ago
answered 14 hours ago
Bejofo
212
212
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f170075%2fnaismiths-rule%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
The test cases outputs all have whole-minute results, is that intentional? Are inputs like
[10], 5125
or[10, 25, 55], 1000
valid and required to be handled?â sundar
20 hours ago
@sundar Yes, they should.
â Okx
14 hours ago