We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Sum of Odd Elements
Sum of Odd Elements
Sort by
recency
|
140 Discussions
|
Please Login in order to post a comment
Here is article related to IOT - Logical Design of IoT
I found a fun thread about coding challenges on the socialmediagirls forum, and it got me thinking about how simple it can be to sum up odd elements in a list!
scala = arr.filter(_ % 2 != 0).foldLeft(0)(_ + _)
def sumofodd(n): a = 0 # Initialize the accumulator for i in range(1, n+1): # Loop through numbers from 1 to n if i % 2 != 0: # Check if the number is odd a += i # Add the odd number to a return a
b = sumofodd(5) print(b)
Scala: arr.filter(x => x%2 != 0).sum