Jami Nishelle's Music and Tech Blog

Read more about my musical process, coding with data structures and algorithms, and using technology to create and market music.

  • Mar 2, 2025

Listening to Running Sum of 1d Array

  • Jami Nishelle
  • 0 comments

Running Sum of 1d Array

Running Sum of 1d Array

This is a Leetcode question that is asking users to create a running sum. Given an array of numbers, create a running sum of the array. For instance, if you have an array of nums = [1,2,3,4], then you would have a running_sum = [1,3,6,10] where you have [1, (1+2), (1+2+3), (1+2+3+4)].

I solved this problem by using a loop that takes each value in the array and adds it to the latest sum. It’s on the order of O(n) for time complexity and space complexity, in which I loop through each element of the array (n is the number of elements).

Thanks for reading Learning Data Structures and Algorithms with Music! Subscribe for free to receive new posts and support my work.

To hear this, I start with 4 MIDI notes, nums = [21,22,23,24] and I convert it to a running sum of MIDI notes, running_sum_final = [21, 43, 66, 90]. Note that MIDI goes from 21 to 108 in terms of the 88 keys of a piano, so 21, 22, 23, and 24 are the lowest notes, and then once we do the running sum, they jump up to higher notes in MIDI.

Here is the code:

# initialize the array of MIDI notes
# Create the running sum array

nums = [21,22,23,24]
running_sum_final = [] # initialize the final list
running_sum = 0 # initialize with 0

for i in nums: 
    running_sum = i + running_sum
    running_sum_final.append(running_sum)  

print(running_sum_final)  
melody = music21.converter.parseFile('major-scale.mid')
melody.show('midi')
# pip install midiutil 
# pip install music21
# import the library  
from midiutil import MIDIFile

# Create the MIDI file with the nums array 

degrees  = nums  # MIDI note number
track    = 0
channel  = 0
time     = 0    # In beats

duration = 1    # In beats
tempo    = 60   # In BPM
volume   = 100  # 0-127, as per the MIDI standard

MyMIDI = MIDIFile(1)  # One track, defaults to format 1 (tempo track is created
                      # automatically)
MyMIDI.addTempo(track, time, tempo)

for i, pitch in enumerate(degrees):
    MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)

with open("major-scale.mid", "wb") as output_file:
    MyMIDI.writeFile(output_file)

Play the nums MIDI array

melody = music21.converter.parseFile('major-scale.mid')
melody.show('midi')
# Create the MIDI file with the running sums array

degrees  = running_sum_final  # MIDI note number
track    = 0
channel  = 0
time     = 0    # In beats
duration = 1    # In beats
tempo    = 60   # In BPM
volume   = 100  # 0-127, as per the MIDI standard

MyMIDI = MIDIFile(1)  # One track, defaults to format 1 (tempo track is created
                      # automatically)
MyMIDI.addTempo(track, time, tempo)

for i, pitch in enumerate(degrees):
    MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)

with open("major-scale_running_sum_final.mid", "wb") as output_file:
    MyMIDI.writeFile(output_file) 

Play the running_nums MIDI array

melody = music21.converter.parseFile('major-scale_running_sum_final.mid')
melody.show('midi')

Here is the link to the Jupyter Notebook to play it in your browser. Sounds pretty cool!

Thanks for reading Learning Data Structures and Algorithms with Music! Subscribe for free to receive new posts and support my work.

0 comments

Sign upor login to leave a comment