• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 09:38
CEST 15:38
KST 22:38
  • 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
TL.net Map Contest #21: Voting3[ASL20] Ro4 Preview: Descent6Team TLMC #5: Winners Announced!3[ASL20] Ro8 Preview Pt2: Holding On9Maestros of the Game: Live Finals Preview (RO4)5
Community News
Weekly Cups (Oct 6-12): Four star herO65.0.15 Patch Balance Hotfix (2025-10-8)70Weekly Cups (Sept 29-Oct 5): MaxPax triples up3PartinG joins SteamerZone, returns to SC2 competition325.0.15 Balance Patch Notes (Live version)119
StarCraft 2
General
5.0.15 Patch Balance Hotfix (2025-10-8) The New Patch Killed Mech! TL.net Map Contest #21: Voting Weekly Cups (Oct 6-12): Four star herO PartinG joins SteamerZone, returns to SC2 competition
Tourneys
Master Swan Open (Global Bronze-Master 2) Tenacious Turtle Tussle WardiTV Mondays SC2's Safe House 2 - October 18 & 19 Sparkling Tuna Cup - Weekly Open Tournament
Strategy
Custom Maps
External Content
Mutation # 495 Rest In Peace Mutation # 494 Unstable Environment Mutation # 493 Quick Killers Mutation # 492 Get Out More
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ Brood War web app to calculate unit interactions BSL Season 21 Whose hotkey signature is this? Any rep analyzer that shows resources situation?
Tourneys
[ASL20] Semifinal B What should Snow play on Roaring Currents? [ASL20] Semifinal A [Megathread] Daily Proleagues
Strategy
Current Meta BW - ajfirecracker Strategy & Training Siegecraft - a new perspective TvZ Theorycraft - Improving on State of the Art
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread ZeroSpace Megathread Dawn of War IV Path of Exile
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
SPIRED by.ASL Mafia {211640} TL Mafia Community Thread
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Men's Fashion Thread Things Aren’t Peaceful in Palestine Sex and weight loss
Fan Clubs
The herO Fan Club! The Happy Fan Club!
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023 NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Recent Gifted Posts
Blogs
Inbreeding: Why Do We Do It…
Peanutsc
From Tilt to Ragequit:The Ps…
TrAiDoS
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1180 users

The Big Programming Thread - Page 885

Forum Index > General Forum
Post a Reply
Prev 1 883 884 885 886 887 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.
Acrofales
Profile Joined August 2010
Spain18088 Posts
May 31 2017 17:00 GMT
#17681
On June 01 2017 01:00 Hanh wrote:
What would an algorithmic solution be?


What the guys above you did.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-05-31 19:40:32
May 31 2017 19:38 GMT
#17682
Ok we've been given homework #1.

It is okay for me to post homeworks, and to discuss them. I figure you guys will probably find this fun so I am posting it, I'll post my attempt before I read any solutions.


Assume you have a necklace of stones. Some of the stones have positive value and some
have negative value. You have the opportunity to snip the necklace in two places (creating two
bands) and weld the endpoints of one of the two bands back into a necklace. You would like
your new necklace to be as valuable as possible. You can assume the necklace has n stones
with values v[0], v[1], . . . , v[n − 1].


ah, so we have a circular connecting necklace and you snip it in 2 places, using one of the new bands to make a new necklace


(a) Give an algorithm to find the value of the new necklace. If all of the stones have negative
value your answer should be 0. Make your algorithm as clean and elegant as possible.



(b) Give an algorithm to determine where you should snip the original necklace (not just its
value). Make your algorithm as clean and elegant as possible. If all of the values are
positive you should not snip and your algorithm should print:
Do not snip.
If all of the values are negative you should not snip and your algorithm should print:
Throw necklace away.
If possible the algorithm should determine these two situations without explicitly checking
for them.

CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
Last Edited: 2017-05-31 19:50:55
May 31 2017 19:49 GMT
#17683
What the heck kind of homework question is that lol. My eyes glaze over trying to read the paragraphs. Sounds like a pretty hard problem. Looks like a linked list, or an array kind of problem. Linked list would probably be the easiest to write code for.

"Some have positive, some have negative", why can't we say signed integers? It's just signed integers.

It sounds like one of those "maximal subset" problems. I hate those problems man. Actually this problem here is my least favorite programming question I have ever heard, and sounds pretty similar to yours.

Edit: When it says clean and elegant, what do those terms even mean. Those are really subjective terms. It sort of sounds like "try to find the trick that makes this problem easy to solve", like the trick in the wikipedia page.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
May 31 2017 19:57 GMT
#17684
When he says clean and elegant I imagine he is hinting that there is at least one efficient solution that will not be super complicated, and he wants that solution.

I think that interestingly enough this question is almost the opposite of question 1 of the 3 questions I posted back a little bit ago.

For the sake of familiarity I would view the necklace as a circular array (just "decide" that the ends connect)
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2017-05-31 20:07:10
May 31 2017 20:05 GMT
#17685
Overcomplicating it a bit, the problem simply boils down to the maximum subarray problem.

Of course, part of the course is learning how to decipher word problems.
There is no one like you in the universe.
Acrofales
Profile Joined August 2010
Spain18088 Posts
Last Edited: 2017-05-31 20:14:16
May 31 2017 20:05 GMT
#17686
Sounds like dynamic programming to me. Did you learn dynamic programming yet?

E: re Blisse
+ Show Spoiler +

Yeah. That. Just have to make sure you copy the values twice, so you get the fact that the first and last element are connected. Alternatively you could loop an extra time at the end until your previous value is greater or equal to the newest one.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-05-31 20:23:18
May 31 2017 20:21 GMT
#17687
It isn't the same problem though, I think. I haven't started it yet but I think for this one we actually need to find the minimum subarray. Finding the maximum could give us an incorrect result.

For example if our necklace is:

5 7 -2 5 -2
and then we return back the whole necklace - we are wrong. (or if you don't like this example, replace with much bigger more complicated examples)


I know of dynamic programming but I haven't really done much of it in practice. I think I will be okay though since I know the premise behind it.
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
May 31 2017 20:29 GMT
#17688
What's wrong with returning the whole array?
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
May 31 2017 20:32 GMT
#17689
in the example above the correct answer would be to remove an instance of negative 2 and return everything else, ie: 5 7 5 -2
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
Last Edited: 2017-05-31 20:35:42
May 31 2017 20:34 GMT
#17690
Minimum subarray means lowest value, so -2. Maximum means 5 7 -2 5, for value of 15 (like you correctly pointed out).
Acrofales
Profile Joined August 2010
Spain18088 Posts
Last Edited: 2017-05-31 20:39:51
May 31 2017 20:38 GMT
#17691
On June 01 2017 05:29 CecilSunkure wrote:
What's wrong with returning the whole array?

In that example? It doesn't maximize the value. By just returning [5, 7] you get a value of 12. If you add negative values, your value isn't maximal.

@Travis: you don't want a minimal length. You don't want maximal length. You want maximal value, which is what blisse's link describes.

E: I read -5 for the second 5 :p. But even so you don't want to return the whole array
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-05-31 20:46:18
May 31 2017 20:43 GMT
#17692
I didn't actually realize he had posted a link, it seems to be the exact same color as normal text for me. I thought he was referring to a problem I had posted yesterday.

Which, actually is that problem. But making it circular changes it a bit.

I know we don't want maximal or minimal length, but I am now seeing there really is no difference between looking for minimum subarray or looking for maximum subarray.
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
May 31 2017 20:44 GMT
#17693
Yep that's right, maximum and minimum subarrays can have any length, because the values are what determine max/min, not the element indices or how many elements the subarray has.

And yeah... TL links seem to have lost their luster. It's impossible to see them.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-05-31 20:59:09
May 31 2017 20:53 GMT
#17694
okay now that I am actually working on this it is a bit harder than what I thought it was.

I don't think Blisse's link does it justice - the trouble for me is in handling that it is circular.

edit: I looked up an answer and it's really cute - you guys were underselling this problem

a hint is that my intuition was right that I needed to find the maximum negative subarray, but there is more to it than that.
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
May 31 2017 21:37 GMT
#17695
lol well i'm glad you're enjoying it. You model student you
Acrofales
Profile Joined August 2010
Spain18088 Posts
Last Edited: 2017-05-31 23:02:58
May 31 2017 21:54 GMT
#17696
Pretty sure you can solve it just fine as follows:

+ Show Spoiler +


neckarray = 2*necklace

maxstart = 0
maxend = 0
maxval = 0

tempstart = 0
tempval = 0

for index, value in neckarray:
tempval = max(value, tempval + value);
if tempval == value:
tempstart = index

maxval = max(tempval, maxval)
if maxval == tempval:
maxstart = tempstart
maxend = index +1

if maxend - maxstart > len(necklace):
print "Don't cut"
elif maxval == 0:
print "Trash"
else:
maxend = maxend%len(necklace)
maxstart = maxstart%len(necklace)
print("cut at", maxstart, maxend, "for a necklace with value", maxval)

Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-05-31 22:19:45
May 31 2017 22:14 GMT
#17697
edit2:

I don't know what language that is, but I am guessing what you are doing is

1.) make an array that repeats the original array once
2.) find the maximum continuous subarray.
3.) if the maximum continuous subarray is greater in length than the original array, don't cut

{5, 5, -1, 5, -1, 5} would return "don't cut", which would be wrong because the correct answer would remove an instance of -1
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2017-05-31 22:43:40
May 31 2017 22:39 GMT
#17698
@travis, haven't walked through, but arcofales's code repeats the original array twice, and performs the maximal subarray code on that repeated array.

Seems to be missing a check to ensure that the selected maximum subarray is only of length N though.

edit: oo, neat lol
There is no one like you in the universe.
Acrofales
Profile Joined August 2010
Spain18088 Posts
Last Edited: 2017-05-31 22:44:50
May 31 2017 22:40 GMT
#17699
On June 01 2017 07:14 travis wrote:
edit2:

I don't know what language that is, but I am guessing what you are doing is

1.) make an array that repeats the original array once
2.) find the maximum continuous subarray.
3.) if the maximum continuous subarray is greater in length than the original array, don't cut

{5, 5, -1, 5, -1, 5} would return "don't cut", which would be wrong because the correct answer would remove an instance of -1


It is pseudopython. Mostly python, but too lazy to make it properly. And yeah, completely interested code.

Unless I made a mistake somewhere, it'd return 5, 4 as the start and end for your cut, which is what you'd want. The loop would end with 5, 10 as the maxstart, maxend. Those would then be adjusted (just the maxend in this case) to not be greater than the necklace's length.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-05-31 23:01:11
May 31 2017 22:58 GMT
#17700
Can you explain why it would return 5, 10 as max start and end instead of 0, 10 ?

I thought I had a clue reading that code but maybe I don't, lol
Prev 1 883 884 885 886 887 1032 Next
Please log in or register to reply.
Live Events Refresh
LiuLi Cup
11:00
46
Clem vs ClassicLIVE!
WardiTV898
RotterdaM478
Rex152
TKL 26
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 478
Rex 152
ProTech90
TKL 26
LamboSC2 9
StarCraft: Brood War
Calm 10604
Rain 3320
Horang2 1334
BeSt 992
EffOrt 683
Mini 638
actioN 503
firebathero 437
Larva 424
Stork 372
[ Show more ]
Light 371
Hyun 241
Pusan 183
ZerO 172
Barracks 149
ggaemo 123
PianO 89
Backho 87
Sharp 78
hero 69
scan(afreeca) 66
ToSsGirL 66
Sea.KH 61
JYJ59
Rush 54
Aegong 52
Mong 43
Killer 31
soO 28
zelot 23
sas.Sziky 22
Sacsri 21
JulyZerg 18
HiyA 16
ajuk12(nOOB) 13
SilentControl 12
Bale 11
Terrorterran 11
ivOry 10
Hm[arnc] 9
Noble 9
sorry 4
Dota 2
Gorgc4379
qojqva2794
Dendi1096
XaKoH 366
XcaliburYe249
BananaSlamJamma185
syndereN148
Counter-Strike
pashabiceps296
oskar76
edward26
Other Games
olofmeister948
hiko571
B2W.Neo394
crisheroes366
Lowko308
DeMusliM280
Fuzer 77
ArmadaUGS76
ZerO(Twitch)4
Codebar3
Organizations
Counter-Strike
PGL6590
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 12 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• HerbMon 10
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos3147
Upcoming Events
OSC
22m
OSC
4h 22m
MaxPax vs Gerald
Solar vs Krystianer
PAPI vs Lemon
Ryung vs Moja
Nice vs NightPhoenix
Cham vs TBD
MaNa vs TriGGeR
PiGosaur Monday
10h 22m
OSC
1d 9h
The PondCast
1d 20h
OSC
1d 22h
Wardi Open
2 days
CranKy Ducklings
3 days
Safe House 2
4 days
Sparkling Tuna Cup
4 days
[ Show More ]
Safe House 2
5 days
Liquipedia Results

Completed

Acropolis #4 - TS2
WardiTV TLMC #15
HCC Europe

Ongoing

BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
C-Race Season 1
IPSL Winter 2025-26
EC S1
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025

Upcoming

SC4ALL: Brood War
BSL Season 21
BSL 21 Team A
RSL Offline Finals
RSL Revival: Season 3
Stellar Fest
SC4ALL: StarCraft II
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
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 © 2025 TLnet. All Rights Reserved.