Skip to content

Instantly share code, notes, and snippets.

@thomasjpfan
Last active December 29, 2015 09:59
Show Gist options
  • Save thomasjpfan/7654191 to your computer and use it in GitHub Desktop.
Save thomasjpfan/7654191 to your computer and use it in GitHub Desktop.
Script to obtain optimized Metrocard rides.
# number of optimized prices
num = 15
price_per_ride = 250 #cents
percentage = 5
min_price = 500 #cent
incease_percent = (100+percentage)/100.0
new_price_per_ride = price_per_ride/incease_percent
# Calculate lowest number of rides to satisfy low price
i = int(min_price/new_price_per_ride)
# Generate optimized prices
prices = []
while len(prices) < num:
i = i + 1
sale_price = int(round(new_price_per_ride*i))
value_price = int(round(sale_price*incease_percent))
if sale_price % 5 == 0 and value_price % 250 == 0:
prices.append([i, sale_price/100.0])
# Output to CSV
import csv
header = ['Number of Rides', 'Optimized Price']
with open('prices.csv', 'wb') as csvfile:
filewriter = csv.writer(csvfile)
filewriter.writerow(header)
for price in prices:
filewriter.writerow(price)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment