• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 07:59
CEST 13:59
KST 20:59
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Serral wins HomeStory Cup 2914Serral wins Maestros of the Game 243ByuL, and the Limitations of Standard Play3Team Liquid Map Contest #22: Results and Winners7Code S Season 2 (2026): RO4 and Finals Preview12
Community News
ZeroSpace Early Access is Now Live!19Weekly Cups (July 13-19): Terran & Protoss rise; Zerg falters2Balance hotfix patch 5.0.16b (July 16)88Reynor: GSL Loss Wasn't About Preparation Format16[IPSL] Spring 2026 Grand Finals - This Weekend!18
StarCraft 2
General
Balance hotfix patch 5.0.16b (July 16) How would you feel about frequent/monthly balance patches for SC2? Clem: "I don't have that much hope in Blizzard" Weekly Cups (July 13-19): Terran & Protoss rise; Zerg falters [D] Wireframe Casting Removed
Tourneys
IntoTheTV X SOOP SC2 League : Weekly & Monthly INu's Battles#18 - Cure, herO, Rogue & ByuN RSL Revival: Season 6 - Qualifiers and Main Event Master Swan Open (Global Bronze-Master 2) WardiTV Summer Cup 2026
Strategy
[G] Having the right mentality to improve
Custom Maps
[M] (2) Industrial Park New Map Maker - Looking for Advice - Love or Hate
External Content
Mutation # 535 Assembly of Vengeance The PondCast: SC2 News & Results Mutation # 534 Burning Evacuation Mutation # 533 Die Together
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ Animated Gateway BW General Discussion HORROR STARCRAFT MOVIE How Famous was FlaSh before his Debut?
Tourneys
Escore Tournament - Season 3 [Megathread] Daily Proleagues [IPSL] Spring 2026 Grand Finals - This Weekend! Small VOD Thread 2.0
Strategy
Simple Questions, Simple Answers PvT advise for noobs Fighting Spirit mining rates Creating a full chart of Zerg builds
Other Games
General Games
Path of Exile ZeroSpace Early Access is Now Live! Nintendo Switch Thread General RTS Discussion Thread Diablo IV
Dota 2
Looking for a Dota Mentor Official 'what is Dota anymore' discussion
League of Legends
TSM pausing esports and CLG Dead
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Power Rank NeO.D_StephenKing vs This Guy From 1 Million Dance TL Mafia Community Thread Vanilla Mini Mafia
Community
General
Artificial Intelligence Thread US Politics Mega-thread Russo-Ukrainian War Thread How to buy a book - shipping from Korea to Europe The Games Industry And ATVI
Fan Clubs
The IdrA Fan Club The HerO Fan Club!
Media & Entertainment
Anime Discussion Thread Series you have seen recently... Movie Discussion! [Req][Books] Good Fantasy/SciFi books
Sports
2024 - 2026 Football Thread TeamLiquid Health and Fitness Initiative For 2023 Formula 1 Discussion MLB/Baseball 2023 McBoner: A hockey love story
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread Simple Questions Simple Answers FPS when play League Of Legend on laptop
TL Community
Northern Ireland Global Starcraft The Automated Ban List
Blogs
How Games can Help with Majo…
TrAiDoS
Hello guys!
LIN1s
ASL S22 English Commentary…
namkraft
Poker (part 2)
Nebuchad
An Exploration of th…
waywardstrategy
Customize Sidebar...

Website Feedback

Closed Threads



Active: 3995 users

The Big Programming Thread - Page 534

Forum Index > General Forum
Post a Reply
Prev 1 532 533 534 535 536 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Days
Profile Joined February 2010
United States219 Posts
October 23 2014 21:47 GMT
#10661
Yes I am using that API. Sorry about not providing links. I guess I just don't have experience serializing/deserializing JSON data in C#. I guess I have to do a bit more research xD
We buy things we don't need, with money we don't have, to impress people we don't like.
phar
Profile Joined August 2011
United States1080 Posts
October 24 2014 01:27 GMT
#10662
On October 23 2014 23:48 Morfildur wrote:
Show nested quote +
On October 23 2014 22:41 Manit0u wrote:
On October 23 2014 21:00 Isualin wrote:
On October 23 2014 19:11 CatNzHat wrote:
On October 23 2014 15:54 Manit0u wrote:
On October 23 2014 00:04 Isualin wrote:
On October 23 2014 00:00 zatic wrote:
What is a good HTML text editor these days? Ideally free, with standard features like bracket matching, autocompletion, etc?

sublime text has auto completion and bracket matching for html. also it is fast and has a good ui imo


SublimeText isn't free. I'd go with BlueFish or Komodo Edit.

Atom, better version of sublime IMO

Atom is really slow though. You can see the difference if you use both of them.


Everything seems so slow when you use vim...


Indeed. gVim is even a decent implementation for windows.

For big projects I'd still use a real IDE though.

Depends on the size of the project, haha. There is an upper bound where real IDEs start breaking and you end up back using vim.
Who after all is today speaking about the destruction of the Armenians?
icystorage
Profile Blog Joined November 2008
Jollibee19350 Posts
October 24 2014 03:14 GMT
#10663
In android, the navigation drawer (seen here http://developer.android.com/design/patterns/navigation-drawer.html) when opened, overlaps on the view. How do I change this so that when the drawer is opened it pushes the view to the side instead of overlapping.
LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2014-10-24 06:58:43
October 24 2014 05:46 GMT
#10664
(c# xaml) has anyone bound a viewmodel to a usercontrol before? for some reason it's not working for me ;-;

it was working, i'm just a bad coder (fu mode=twoway)
There is no one like you in the universe.
bangsholt
Profile Joined June 2011
Denmark138 Posts
October 24 2014 20:55 GMT
#10665
On October 24 2014 06:47 Days wrote:
Yes I am using that API. Sorry about not providing links. I guess I just don't have experience serializing/deserializing JSON data in C#. I guess I have to do a bit more research xD


A few helpers then ;-)

https://www.nuget.org/packages/Microsoft.Net.Http

Specifically, HttpClient. Can do all sorts of requests and is async.

https://www.nuget.org/packages/Newtonsoft.Json/

JsonConverter is the best

http://json2csharp.com/

Saves time in creating objects to deserialize to.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
October 24 2014 22:46 GMT
#10666
Hi all - there was some cs problem I got asked - completely bungled it when asked - but I'm trying to work out a solution for it now. Wondered if anyone could check my solution for me.

Question is given an array of stock prices, indexed by date, return the pair of dates where you could buy/sell stock for biggest differential. If there are multiple valid dates, any pair works.

When I got asked the question, I did not ask what happens if there was no day you could have made a profit selling it (i.e. the array is continuously decreasing). I'm a little unsure as to how I'd account for that. The code I have so far definitely doesn't take that into account.

+ Show Spoiler +


from test import rand_array


def min_max(c):
min_idx = 0
min_val = c[0]

max_can_idx = 0
min_can_idx = 0

max_diff = 0

for idx, val in enumerate(c):
if val < min_val:
min_idx = idx
min_val = val
elif val - min_val > max_diff:
max_diff = val - min_val
max_can_idx = idx
min_can_idx = min_idx

return min_can_idx, max_can_idx


if __name__ == '__main__':
arr = rand_array() # rand_array(size=10, min_val=0, max_val=10):
mi, ma = min_max(arr)

print(arr)
print(mi, ma)








RIP GOMTV. RIP PROLEAGUE.
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2014-10-25 04:34:02
October 25 2014 04:09 GMT
#10667
On October 25 2014 05:55 bangsholt wrote:
Show nested quote +
On October 24 2014 06:47 Days wrote:
Yes I am using that API. Sorry about not providing links. I guess I just don't have experience serializing/deserializing JSON data in C#. I guess I have to do a bit more research xD


A few helpers then ;-)

https://www.nuget.org/packages/Microsoft.Net.Http

Specifically, HttpClient. Can do all sorts of requests and is async.

https://www.nuget.org/packages/Newtonsoft.Json/

JsonConverter is the best

http://json2csharp.com/

Saves time in creating objects to deserialize to.


What does Microsoft.Net.Http offer over using System.Net.WebClient and System.Net.Http.HttpClient? Never really used the Microsoft.Net offering.

I prefer using the JToken.FromObject and JToken.ToObject methods instead. Makes code much more portable I feel.

Interestingly, Visual Studio (at the very least 2013) has the ability to create objects too in the Edit menu. I knew people at Microsoft who didn't know about this either haha.


On October 25 2014 07:46 teamamerica wrote:
Hi all - there was some cs problem I got asked - completely bungled it when asked - but I'm trying to work out a solution for it now. Wondered if anyone could check my solution for me.

Question is given an array of stock prices, indexed by date, return the pair of dates where you could buy/sell stock for biggest differential. If there are multiple valid dates, any pair works.

When I got asked the question, I did not ask what happens if there was no day you could have made a profit selling it (i.e. the array is continuously decreasing). I'm a little unsure as to how I'd account for that. The code I have so far definitely doesn't take that into account.



Super easy and more "pythonic" way of doing this is using Python's itertools.combinations(stocks, 2) to generate all possible 2-tuple combinations of stock prices, and finding the max difference of 2-tuples.

To handle the no profit case you can simply not give an answer if the best option is not a profit, or just give the least negative number. The best pair of dates to buy/sell stock can be interpreted as the best days to buy/sell, and if it's impossible to gain a profit, the best days to buy/sell are the days where you lose the least. Depends on how you define the requirements.
There is no one like you in the universe.
dae
Profile Joined June 2010
Canada1600 Posts
Last Edited: 2014-10-25 06:44:52
October 25 2014 06:32 GMT
#10668
On October 25 2014 13:09 Blisse wrote:
Show nested quote +
October 25 2014 07:46 teamamerica wrote:
Hi all - there was some cs problem I got asked - completely bungled it when asked - but I'm trying to work out a solution for it now. Wondered if anyone could check my solution for me.

Question is given an array of stock prices, indexed by date, return the pair of dates where you could buy/sell stock for biggest differential. If there are multiple valid dates, any pair works.

When I got asked the question, I did not ask what happens if there was no day you could have made a profit selling it (i.e. the array is continuously decreasing). I'm a little unsure as to how I'd account for that. The code I have so far definitely doesn't take that into account.



Super easy and more "pythonic" way of doing this is using Python's itertools.combinations(stocks, 2) to generate all possible 2-tuple combinations of stock prices, and finding the max difference of 2-tuples.

To handle the no profit case you can simply not give an answer if the best option is not a profit, or just give the least negative number. The best pair of dates to buy/sell stock can be interpreted as the best days to buy/sell, and if it's impossible to gain a profit, the best days to buy/sell are the days where you lose the least. Depends on how you define the requirements.


Personally, I would:

Sort the array by date, in increasing order (first day first, etc).

Do one linear sweep through the array, keeping track of the minimum price, and the best buy/sell pair seen so far.

That is, for each date, check if it's price is lower then current minimum - if it is, set that date to be minimum. Otherwise, check if the difference between its price/minimum price is larger then the current best buy/sell pair - if it is. set the current best size pair to that date and the current minimum date.

O(nlogn) if the array is not presorted, O(n) if it is.

Edit: Didnt see your code at first, it's pretty much exactly what I just described. (read your question in above posters quote)
teamamerica
Profile Blog Joined July 2010
United States958 Posts
Last Edited: 2014-10-25 06:38:02
October 25 2014 06:36 GMT
#10669
On October 25 2014 13:09 Blisse wrote:
Show nested quote +
On October 25 2014 05:55 bangsholt wrote:
On October 24 2014 06:47 Days wrote:
Yes I am using that API. Sorry about not providing links. I guess I just don't have experience serializing/deserializing JSON data in C#. I guess I have to do a bit more research xD


A few helpers then ;-)

https://www.nuget.org/packages/Microsoft.Net.Http

Specifically, HttpClient. Can do all sorts of requests and is async.

https://www.nuget.org/packages/Newtonsoft.Json/

JsonConverter is the best

http://json2csharp.com/

Saves time in creating objects to deserialize to.


What does Microsoft.Net.Http offer over using System.Net.WebClient and System.Net.Http.HttpClient? Never really used the Microsoft.Net offering.

I prefer using the JToken.FromObject and JToken.ToObject methods instead. Makes code much more portable I feel.

Interestingly, Visual Studio (at the very least 2013) has the ability to create objects too in the Edit menu. I knew people at Microsoft who didn't know about this either haha.


Show nested quote +
On October 25 2014 07:46 teamamerica wrote:
Hi all - there was some cs problem I got asked - completely bungled it when asked - but I'm trying to work out a solution for it now. Wondered if anyone could check my solution for me.

Question is given an array of stock prices, indexed by date, return the pair of dates where you could buy/sell stock for biggest differential. If there are multiple valid dates, any pair works.

When I got asked the question, I did not ask what happens if there was no day you could have made a profit selling it (i.e. the array is continuously decreasing). I'm a little unsure as to how I'd account for that. The code I have so far definitely doesn't take that into account.



Super easy and more "pythonic" way of doing this is using Python's itertools.combinations(stocks, 2) to generate all possible 2-tuple combinations of stock prices, and finding the max difference of 2-tuples.

To handle the no profit case you can simply not give an answer if the best option is not a profit, or just give the least negative number. The best pair of dates to buy/sell stock can be interpreted as the best days to buy/sell, and if it's impossible to gain a profit, the best days to buy/sell are the days where you lose the least. Depends on how you define the requirements.


So I understand that algorithmic complexity isn't the be-all,end-all of code, and that stuff like readability. But wouldn't that be n^2 and what I posed be O(n). I mean itertools.combinations(stocks,2) is basically nested for-loops and then iterating over that, which definitely is simplest.

Secondly, that doesn't seem right because because the order of stocks matter - you since stock indexed by date, min_idx has to be less then max_idx (can't buy stock in future to sell in past). Just generating all combinations wouldn't take this into account I think.

edit:
On October 25 2014 15:32 dae wrote:
Show nested quote +
On October 25 2014 13:09 Blisse wrote:
October 25 2014 07:46 teamamerica wrote:
Hi all - there was some cs problem I got asked - completely bungled it when asked - but I'm trying to work out a solution for it now. Wondered if anyone could check my solution for me.

Question is given an array of stock prices, indexed by date, return the pair of dates where you could buy/sell stock for biggest differential. If there are multiple valid dates, any pair works.

When I got asked the question, I did not ask what happens if there was no day you could have made a profit selling it (i.e. the array is continuously decreasing). I'm a little unsure as to how I'd account for that. The code I have so far definitely doesn't take that into account.



Super easy and more "pythonic" way of doing this is using Python's itertools.combinations(stocks, 2) to generate all possible 2-tuple combinations of stock prices, and finding the max difference of 2-tuples.

To handle the no profit case you can simply not give an answer if the best option is not a profit, or just give the least negative number. The best pair of dates to buy/sell stock can be interpreted as the best days to buy/sell, and if it's impossible to gain a profit, the best days to buy/sell are the days where you lose the least. Depends on how you define the requirements.


Personally, I would:

Sort the array by date, in increasing order (first day first, etc).

Do one linear sweep through the array, keeping track of the minimum price, and the best buy/sell pair seen so far.

That is, for each date, check if it's price is lower then current minimum - if it is, set that date to be minimum. Otherwise, check if the difference between its price/minimum price is larger then the current best buy/sell pair - if it is. set the current best size pair to that date and the current minimum date.

O(nlogn) if the array is not presorted, O(n) if it is.



Question assumes array sorted by date. Isn't this what I posted???
RIP GOMTV. RIP PROLEAGUE.
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
Last Edited: 2014-10-25 06:57:29
October 25 2014 06:53 GMT
#10670
--- Nuked ---
bangsholt
Profile Joined June 2011
Denmark138 Posts
October 25 2014 07:37 GMT
#10671
On October 25 2014 13:09 Blisse wrote:
Show nested quote +
On October 25 2014 05:55 bangsholt wrote:
On October 24 2014 06:47 Days wrote:
Yes I am using that API. Sorry about not providing links. I guess I just don't have experience serializing/deserializing JSON data in C#. I guess I have to do a bit more research xD


A few helpers then ;-)

https://www.nuget.org/packages/Microsoft.Net.Http

Specifically, HttpClient. Can do all sorts of requests and is async.

https://www.nuget.org/packages/Newtonsoft.Json/

JsonConverter is the best

http://json2csharp.com/

Saves time in creating objects to deserialize to.


What does Microsoft.Net.Http offer over using System.Net.WebClient and System.Net.Http.HttpClient? Never really used the Microsoft.Net offering.

I prefer using the JToken.FromObject and JToken.ToObject methods instead. Makes code much more portable I feel.


I was referring to System.Net.Http.HttpClient ;o)

Is JToken a standard interface of some sort, or why do you think it makes code more portable than JsonConverter?

I mostly care about compatibility with Xamarin.
Xyik
Profile Blog Joined November 2009
Canada728 Posts
October 25 2014 08:57 GMT
#10672
On October 25 2014 15:53 Nesserev wrote:
Show nested quote +
On October 25 2014 13:09 Blisse wrote:
On October 25 2014 07:46 teamamerica wrote:
Hi all - there was some cs problem I got asked - completely bungled it when asked - but I'm trying to work out a solution for it now. Wondered if anyone could check my solution for me.

Question is given an array of stock prices, indexed by date, return the pair of dates where you could buy/sell stock for biggest differential. If there are multiple valid dates, any pair works.

When I got asked the question, I did not ask what happens if there was no day you could have made a profit selling it (i.e. the array is continuously decreasing). I'm a little unsure as to how I'd account for that. The code I have so far definitely doesn't take that into account.



Super easy and more "pythonic" way of doing this is using Python's itertools.combinations(stocks, 2) to generate all possible 2-tuple combinations of stock prices, and finding the max difference of 2-tuples.

To handle the no profit case you can simply not give an answer if the best option is not a profit, or just give the least negative number. The best pair of dates to buy/sell stock can be interpreted as the best days to buy/sell, and if it's impossible to gain a profit, the best days to buy/sell are the days where you lose the least. Depends on how you define the requirements.

Won't you ignore the fact that the days are in a certain order... you cannot sell something before you bought it.

Observe this list of stock prices (for consecutive days):
5	3	6	7	8	4	2	1	3


First day, we start at 5, which is our lowest, and highest sp (stock price) so far:
lowest_sp = 5, lowest_day = 0
highest_sp = 5, highest_day = 0
max_diff = 0, buy_day = 0, sell_day = 0


3 OPTIONS:
- Whenever the sp is lower than lowest_sp, set lowest_x and highest_x to current day (day 2)
lowest_sp = 3, lowest_day = 1
highest_sp = 3, highest_day = 1
max_diff = 0, buy_day = 0, sell_day = 0


- Whenever the sp is higher than highest_sp, set highest_x to current day, ... (day 3)
lowest_sp = 3, lowest_day = 1
highest_sp = 6, highest_day = 2
max_diff = 0, buy_day = 0, sell_day = 0

... and compare the difference between lowest_sp and highest_sp with max_diff.

If the difference is larger than max_diff, you have a new max_diff, buy day = lowest_day and sell_day = highest_day:
lowest_sp = 3, lowest_day = 1
highest_sp = 6, highest_day = 2
max_diff = 3, buy_day = 1, sell_day = 2


- Otherwise:
pass go to next day...

The idea behind this algorithm, is that if the stock price on the current day is lower than any following day, it will definitely be the buy day for stocks sold on any following day. Can anyone see any flaws in this approach, or does anyone know a better algorithm?

EDIT: Why do people always post 5 seconds just after I decide to reply to something


This approach is correct.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
October 25 2014 17:35 GMT
#10673
On October 25 2014 15:53 Nesserev wrote:
Show nested quote +
On October 25 2014 13:09 Blisse wrote:
On October 25 2014 07:46 teamamerica wrote:
Hi all - there was some cs problem I got asked - completely bungled it when asked - but I'm trying to work out a solution for it now. Wondered if anyone could check my solution for me.

Question is given an array of stock prices, indexed by date, return the pair of dates where you could buy/sell stock for biggest differential. If there are multiple valid dates, any pair works.

When I got asked the question, I did not ask what happens if there was no day you could have made a profit selling it (i.e. the array is continuously decreasing). I'm a little unsure as to how I'd account for that. The code I have so far definitely doesn't take that into account.



Super easy and more "pythonic" way of doing this is using Python's itertools.combinations(stocks, 2) to generate all possible 2-tuple combinations of stock prices, and finding the max difference of 2-tuples.

To handle the no profit case you can simply not give an answer if the best option is not a profit, or just give the least negative number. The best pair of dates to buy/sell stock can be interpreted as the best days to buy/sell, and if it's impossible to gain a profit, the best days to buy/sell are the days where you lose the least. Depends on how you define the requirements.

Won't you ignore the fact that the days are in a certain order... you cannot sell something before you bought it.

Observe this list of stock prices (for consecutive days):
5	3	6	7	8	4	2	1	3


First day, we start at 5, which is our lowest, and highest sp (stock price) so far:
lowest_sp = 5, lowest_day = 0
highest_sp = 5, highest_day = 0
max_diff = 0, buy_day = 0, sell_day = 0


3 OPTIONS:
- Whenever the sp is lower than lowest_sp, set lowest_x and highest_x to current day (day 2)
lowest_sp = 3, lowest_day = 1
highest_sp = 3, highest_day = 1
max_diff = 0, buy_day = 0, sell_day = 0


- Whenever the sp is higher than highest_sp, set highest_x to current day, ... (day 3)
lowest_sp = 3, lowest_day = 1
highest_sp = 6, highest_day = 2
max_diff = 0, buy_day = 0, sell_day = 0

... and compare the difference between lowest_sp and highest_sp with max_diff.

If the difference is larger than max_diff, you have a new max_diff, buy day = lowest_day and sell_day = highest_day:
lowest_sp = 3, lowest_day = 1
highest_sp = 6, highest_day = 2
max_diff = 3, buy_day = 1, sell_day = 2


- Otherwise:
pass go to next day...

The idea behind this algorithm, is that if the stock price on the current day is lower than any following day, it will definitely be the buy day for stocks sold on any following day. Can anyone see any flaws in this approach, or does anyone know a better algorithm?

EDIT: Why do people always post 5 seconds just after I decide to reply to something


Checked -- pretty sure that's same approach as dae and I take. I don't think that you need to remember both lowest and highest prices, since you are remembering max_diff, so really knowing lowest price and max diff lets you always calculate what sell price you're going with is.

Still doesn't seem to handle case of continuously decreasing array.

Apologies if this isn't what you were proposing.

+ Show Spoiler +


def tl_sol_1(c):
"""Nesserev"""
lowest_sp = c[0]
lowest_day = 0
highest_sp = c[0]
highest_day = 0
max_diff = 0
buy_day = 0
sell_day = 0

for idx, sp in enumerate(c):
if sp < lowest_sp:
lowest_day = idx
highest_day = idx
lowest_sp = sp
highest_sp = sp
elif sp > highest_sp:
highest_sp = sp
highest_day = idx
if highest_sp - lowest_sp > max_diff:
buy_day = lowest_day
sell_day = highest_day
max_diff = highest_sp - lowest_sp

return buy_day, sell_day


But see my adjustments to it, which makes it boil down to what dae and I had.

def tl_sol_1(c):
"""Nesserev"""
lowest_sp = c[0]
lowest_day = 0
# highest_sp = c[0]
# highest_day = 0
max_diff = 0
buy_day = 0
sell_day = 0

for idx, sp in enumerate(c):
if sp < lowest_sp:
lowest_day = idx
# highest_day = idx
lowest_sp = sp
# highest_sp = sp
# elif sp > highest_sp:
# highest_sp = sp
# highest_day =
if sp - lowest_sp > max_diff: # only difference is here is if vs elif...but looking at code that shouldn't be relevant
buy_day = lowest_day
sell_day = idx
max_diff = idx - lowest_sp

return buy_day, sell_day

RIP GOMTV. RIP PROLEAGUE.
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
October 25 2014 17:58 GMT
#10674
--- Nuked ---
teamamerica
Profile Blog Joined July 2010
United States958 Posts
October 25 2014 18:18 GMT
#10675
On October 26 2014 02:58 Nesserev wrote:
Show nested quote +
On October 26 2014 02:35 teamamerica wrote:
Checked -- pretty sure that's same approach as dae and I take. I don't think that you need to remember both lowest and highest prices, since you are remembering max_diff, so really knowing lowest price and max diff lets you always calculate what sell price you're going with is.

Still doesn't seem to handle case of continuously decreasing array.

Apologies if this isn't what you were proposing.

+ Show Spoiler +


def tl_sol_1(c):
"""Nesserev"""
lowest_sp = c[0]
lowest_day = 0
highest_sp = c[0]
highest_day = 0
max_diff = 0
buy_day = 0
sell_day = 0

for idx, sp in enumerate(c):
if sp < lowest_sp:
lowest_day = idx
highest_day = idx
lowest_sp = sp
highest_sp = sp
elif sp > highest_sp:
highest_sp = sp
highest_day = idx
if highest_sp - lowest_sp > max_diff:
buy_day = lowest_day
sell_day = highest_day
max_diff = highest_sp - lowest_sp

return buy_day, sell_day


But see my adjustments to it, which makes it boil down to what dae and I had.

def tl_sol_1(c):
"""Nesserev"""
lowest_sp = c[0]
lowest_day = 0
# highest_sp = c[0]
# highest_day = 0
max_diff = 0
buy_day = 0
sell_day = 0

for idx, sp in enumerate(c):
if sp < lowest_sp:
lowest_day = idx
# highest_day = idx
lowest_sp = sp
# highest_sp = sp
# elif sp > highest_sp:
# highest_sp = sp
# highest_day =
if sp - lowest_sp > max_diff: # only difference is here is if vs elif...but looking at code that shouldn't be relevant
buy_day = lowest_day
sell_day = idx
max_diff = idx - lowest_sp

return buy_day, sell_day


Yeah, you're right. Overall, it's the same approach.

Well, it implicitly handles the case of a continuously decreasing array... because if the stock prices are always decreasing, the max_diff will never change (buy and sell on the first day, zero loss?).

One difference would be the that my approach will try to skip dates that can be ruled out as candidates, while your approach will calculate and compare something for every day.


Ya I guess I can't know what assumptions question had since I didn't ask - but assuming you can't buy-sell on same day, how would you handle that?
RIP GOMTV. RIP PROLEAGUE.
Nesserev
Profile Blog Joined January 2011
Belgium2760 Posts
October 25 2014 18:34 GMT
#10676
--- Nuked ---
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2014-10-25 19:34:35
October 25 2014 19:03 GMT
#10677
Itertools.combinations preserves order of inputs I believe.

My way I write like 4-5 lines of code that solves the abstract problem. If we're not going for fastest code I would prefer this way by far. Even if we were going for fastest this is infinitely more "pythonic" and readable and id use it until I see bottlenecks.

edit: -i like your code better-
There is no one like you in the universe.
berated-
Profile Blog Joined February 2007
United States1134 Posts
Last Edited: 2014-10-25 19:15:26
October 25 2014 19:12 GMT
#10678
On October 24 2014 12:14 icystorage wrote:
In android, the navigation drawer (seen here http://developer.android.com/design/patterns/navigation-drawer.html) when opened, overlaps on the view. How do I change this so that when the drawer is opened it pushes the view to the side instead of overlapping.


Did you already try this solution suggested on stack overflow?

Another try

Did you try seaching? Seems to be a lot of examples.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
October 25 2014 19:14 GMT
#10679
On October 26 2014 04:03 Blisse wrote:
Itertools.combinations preserves order of inputs I believe.

My way I write like 4-5 lines of code that solves the abstract problem. If we're not going for fastest code I would prefer this way by far. Even if we were going for fastest this is infinitely more "pythonic" and readable and id use it until I see bottlenecks.


Definitely see and appreciate that point of view, I had mentioned that.

Didn't realize that combinations preservers order of input, good to know.

Given combinations preserves order of inputs (sry, should have checked on that first), that gives you nice Pytohn one-liner


return max(itertools.combinations(enumerate(c), 2), key=lambda ((i1, v1), (i2, v2)): v2 - v1)
RIP GOMTV. RIP PROLEAGUE.
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2014-10-25 19:37:30
October 25 2014 19:28 GMT
#10680
On October 26 2014 04:14 teamamerica wrote:
Show nested quote +
On October 26 2014 04:03 Blisse wrote:
Itertools.combinations preserves order of inputs I believe.

My way I write like 4-5 lines of code that solves the abstract problem. If we're not going for fastest code I would prefer this way by far. Even if we were going for fastest this is infinitely more "pythonic" and readable and id use it until I see bottlenecks.


Definitely see and appreciate that point of view, I had mentioned that.

Didn't realize that combinations preservers order of input, good to know.

Given combinations preserves order of inputs (sry, should have checked on that first), that gives you nice Pytohn one-liner


return max(itertools.combinations(enumerate(c), 2), key=lambda ((i1, v1), (i2, v2)): v2 - v1)



ye :p

in python i try to avoid writing as much code as possible. so everytime i see procedural code in python i ask myself if i could codegolf it instead
There is no one like you in the universe.
Prev 1 532 533 534 535 536 1032 Next
Please log in or register to reply.
Live Events Refresh
CrankTV Team League
11:00
Crank Gathers S4: Playoffs
LiquipediaDiscussion
KCM Race Survival
10:00
2026 Season 3 Week 3
Kim Chul Min (afreeca) 1555
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Lowko390
RotterdaM 192
Rex 37
StarCraft: Brood War
Rain 5527
Calm 4347
BeSt 2179
910 2177
Bisu 1217
Zeus 1009
Jaedong 733
firebathero 599
Horang2 589
Mini 382
[ Show more ]
EffOrt 285
Soma 244
Soulkey 219
Snow 214
Light 196
Stork 172
actioN 165
Last 127
Dewaltoss 113
Hyun 111
Rush 108
Mong 93
Mind 92
Larva 87
Pusan 76
ggaemo 62
ToSsGirL 51
Sea.KH 35
sorry 33
Shine 30
Bale 29
Movie 29
hero 29
yabsab 25
JYJ 24
[sc1f]eonzerg 23
Sacsri 22
Sexy 21
Barracks 19
Sharp 18
Terrorterran 18
HiyA 17
zelot 16
JulyZerg 16
ajuk12(nOOB) 13
IntoTheRainbow 12
Yoon 10
Noble 8
Dota 2
Dendi513
XcaliburYe122
Counter-Strike
shoxiejesuss942
fl0m889
markeloff468
edward68
Other Games
gofns12170
singsing2199
FrodaN2192
B2W.Neo487
crisheroes291
uThermal171
OGKoka 163
Sick157
Liquid`LucifroN123
CosmosSc2 24
Trikslyr13
Organizations
StarCraft 2
WardiTV68
StarCraft: Brood War
UltimateBattle 36
StarCraft 2
angryscii 21
StarCraft: Brood War
lovetv 14
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 14 non-featured ]
StarCraft 2
• intothetv
• AfreecaTV YouTube
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• C_a_k_e 2179
• WagamamaTV311
League of Legends
• Jankos2007
• TFBlade707
Upcoming Events
WardiTV Summer Champion…
2m
Replay Cast
12h 2m
Escore
22h 2m
CrankTV Team League
23h 2m
Big Brain Bouts
1d 4h
Soulspirit vs goblin
TriGGeR vs Bunny
Korean StarCraft League
1d 15h
Afreeca Starleague
1d 16h
RSL Revival
1d 21h
Serral vs SHIN
herO vs Solar
Online Event
2 days
Replay Cast
2 days
[ Show More ]
RSL Revival
2 days
Clem vs ByuN
Rogue vs Lambo
OSC
3 days
WardiTV Weekly
3 days
Sparkling Tuna Cup
4 days
INu's Battles
4 days
Cure vs herO
ByuN vs Rogue
PiGosaur Cup
5 days
The PondCast
5 days
Kung Fu Cup
5 days
Patches Events
6 days
Replay Cast
6 days
CrankTV Team League
6 days
Liquipedia Results

Completed

Proleague 2026-07-22
HSC XXIX
Eternal Conflict S2 E3

Ongoing

CSL 2026 Summer (S21)
KCM Race Survival 2026 Season 3
RSL Revival: Season 6
CranK Gathers Season 4: BW vs SC2 Team League
SCTL 2026 Spring
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026

Upcoming

Escore Tournament S3: W4
ASL S22 SEASON OPEN Day 2
Escore Tournament S3: W5
ASL Season 22: Qualifier #1
ASL Season 22: Qualifier #2
CSLAN 4
ASL Season 22
HSC XXX
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
Light Tournament 2026
Eternal Conflict S2 Finale
ESL Pro League Season 24
Stake Ranked Episode 4
Logitech G Connect 2026
SL StarSeries Fall 2026
FISSURE Playground #5
BLAST Open Fall 2026
Esports World Cup 2026
BLAST Bounty Summer 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.