• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 09:39
CEST 15:39
KST 22:39
  • 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
[ASL22] Ro16 Preview: Rough Waters9[ASL22] Ro24 Preview: Siren's Call8[ASL22] Ro24 Preview: Summer's End9Serral wins HomeStory Cup 2915Serral wins Maestros of the Game 244
Community News
Weekly Cups (Aug 30-Sep 7): herO thrives amid growing schism5Official StarCraft website teases new content ahead of BlizzCon?118Stellar Fest TWO the Moon (Dec 16-20)9Weekly Cups (August 24-30): Patches' balance mod takes over3New 3v3 BGH Ladder (and more) on ShieldBattery!46
StarCraft 2
General
Weekly Cups (Aug 30-Sep 7): herO thrives amid growing schism Nexon wins bid to develop StarCraft IP content, distribute Overwatch mobile game Balance hotfix patch 5.0.16b (July 16) SC4ALL: II Winner Will Earn a Spot at HSC 30! Weekly Cups (August 24-30): Patches' balance mod takes over
Tourneys
Sea Duckling Open (Global, Bronze-Diamond) 2026 GSTL Announcement Sparkling Tuna Cup - Weekly Open Tournament Stellar Fest TWO the Moon (Dec 16-20) IntoTheTV X SOOP SC2 League : Weekly & Monthly
Strategy
[G] Having the right mentality to improve
Custom Maps
Nexus Wars 2021 GUIDE [M] (2) Industrial Park
External Content
Mutation # 542 The Ascended Mutation # 541 Binary Choice The PondCast: SC2 News & Results Mutation # 540 Dodge This
Brood War
General
Official StarCraft website teases new content ahead of BlizzCon? [ASL22] Ro16 Preview: Rough Waters ASL22 General Discussion Pros React To: Calm's Big Brain Play Vs Mini New 3v3 BGH Ladder (and more) on ShieldBattery!
Tourneys
[ASL22] Ro16 Group B [Megathread] Daily Proleagues 2v2v2v2 Tournament [ASL22] Ro16 Group A
Strategy
Replay Review Process - What do you do? Simple Questions, Simple Answers Odyssey Mineral Stack Saturation Game Theory for Starcraft
Other Games
General Games
Nintendo Switch Thread EVE Corporation Diablo IV [Maplestory Hardcore] Let's Play~!! General RTS Discussion Thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
[TL LoL EUW IHs] Teemo shall perish 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 TL Mafia Community Thread NeO.D_StephenKing vs This Guy From 1 Million Dance
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread European Politico-economics QA Mega-thread Trading/Investing Thread Things Aren’t Peaceful in Palestine
Fan Clubs
MarineLorD Fan Club The Creator Fan Club The ShoWTimE Fan Club
Media & Entertainment
Movie Discussion! Anime Discussion Thread
Sports
Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 MLB/Baseball 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Northern Ireland Global Starcraft
Blogs
F* the Children and Get Off …
TrAiDoS
Dreaming of BW patches (mod…
c3rberUs
Regacy Esports: The Bh…
regacyesports
LOCKPICKING NOOB
LUCKY_NOOB
Customize Sidebar...

Website Feedback

Closed Threads



Active: 6852 users

The Big Programming Thread - Page 826

Forum Index > General Forum
Post a Reply
Prev 1 824 825 826 827 828 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.
Mr. Wiggles
Profile Blog Joined August 2010
Canada5894 Posts
January 10 2017 05:25 GMT
#16501
from collections import Counter

xs = [5, 5, 3, 2, 2]

counts = Counter(xs)

output = [x for x in xs if counts[x] > 1]

The above doesn't require that you sort the list (or that it's already sorted). Assuming Counter() is implemented efficiently, it should run in O(n).

Reference: https://docs.python.org/3/library/collections.html#collections.Counter
you gotta dance
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-01-10 15:00:50
January 10 2017 14:43 GMT
#16502
Now I want to ask about classes in Python. The tutorial I was reading didn't even touch them! So i thought maybe they don't exist? But then how does inheritance work?

So I looked it up and they do exist? But it seems that unlike java, they are not a requirement.

so, separate .py files are "modules", which you can import and use in other python files?

It seems like modules in python are a lot like upper level classes in java.

But classes in python are also similar to classes in java in that you instantiate them? Is that the actual purpose of classes in python? Object instantiation? (or I guess more specifically, defining new types of objects for instantiation)

Also in python if I define a variable at the class level, this variable isn't global, right? It needs to be at the level above that?
Acrofales
Profile Joined August 2010
Spain18433 Posts
January 10 2017 15:12 GMT
#16503
Classes work almost exactly the same as in Java. You're just not forced to use them. And yes, you can have multiple classes together in a single file (module). Modules are nothing like classes. They are more like Java packages, but with some extra functionality (namely, they can contain code that isn't in classes), but most importantly, you don't inherit jack shit from modules.

Scoping in Python is pretty simple and described in the manual, but here's a SO answer that lifts the right part from the manual: http://stackoverflow.com/questions/291978/short-description-of-scoping-rules

Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-01-10 15:56:09
January 10 2017 15:52 GMT
#16504
I am so confused about global variables. I am told this should be intuitive but I don't understand how to do something.

I want a global variable, let's call it myWord.

I want a function that modifies myWord. let's call it myFunction

so my python file could look like


myWord = ""

def myFunction():
global myWord
myWord = myWord + "more words"


this is basically what I am trying to do.
But python doesn't like this. it tells me I need to define(initialize?) the global variable within the function

But that's what I don't understand! if I do that, if I say myWord = "" in my function, then what I am trying to do will be reset every time I run the function!

Also why the heck would I need to initialize it in there anyways, isn't the entire point that it's a global variable and it was created at the global level?
Acrofales
Profile Joined August 2010
Spain18433 Posts
January 10 2017 16:34 GMT
#16505
On January 11 2017 00:52 travis wrote:
I am so confused about global variables. I am told this should be intuitive but I don't understand how to do something.

I want a global variable, let's call it myWord.

I want a function that modifies myWord. let's call it myFunction

so my python file could look like


myWord = ""

def myFunction():
global myWord
myWord = myWord + "more words"


this is basically what I am trying to do.
But python doesn't like this. it tells me I need to define(initialize?) the global variable within the function

But that's what I don't understand! if I do that, if I say myWord = "" in my function, then what I am trying to do will be reset every time I run the function!

Also why the heck would I need to initialize it in there anyways, isn't the entire point that it's a global variable and it was created at the global level?

Works just fine for me... you sure you don't have some other code cluttering up your file?
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-01-10 16:44:30
January 10 2017 16:43 GMT
#16506
On January 11 2017 01:34 Acrofales wrote:
Show nested quote +
On January 11 2017 00:52 travis wrote:
I am so confused about global variables. I am told this should be intuitive but I don't understand how to do something.

I want a global variable, let's call it myWord.

I want a function that modifies myWord. let's call it myFunction

so my python file could look like


myWord = ""

def myFunction():
global myWord
myWord = myWord + "more words"


this is basically what I am trying to do.
But python doesn't like this. it tells me I need to define(initialize?) the global variable within the function

But that's what I don't understand! if I do that, if I say myWord = "" in my function, then what I am trying to do will be reset every time I run the function!

Also why the heck would I need to initialize it in there anyways, isn't the entire point that it's a global variable and it was created at the global level?

Works just fine for me... you sure you don't have some other code cluttering up your file?


oh wow really?

well here is the actual code (im still playing that code challenge solving game, it's pretty fun)


def recall_password(cipher_grille, ciphered_password):

result = ""



def doIt(temp):
global result
j = 0
for w in temp:
j += 1
i = 0
for c in w:
i += 1
if c == "X":
thisString = ciphered_password[j-1]
result = result + thisString[i-1]

doIt(cipher_grille)


here is the error it gives


NameError: name 'result' is not defined
doIt, 18
recall_password, 20


line 18 is: result = result + thisString[i-1]
mantequilla
Profile Blog Joined June 2012
Turkey781 Posts
Last Edited: 2017-01-10 16:45:36
January 10 2017 16:45 GMT
#16507
+ Show Spoiler +
On January 11 2017 00:52 travis wrote:
I am so confused about global variables. I am told this should be intuitive but I don't understand how to do something.

I want a global variable, let's call it myWord.

I want a function that modifies myWord. let's call it myFunction

so my python file could look like


myWord = ""

def myFunction():
global myWord
myWord = myWord + "more words"


this is basically what I am trying to do.
But python doesn't like this. it tells me I need to define(initialize?) the global variable within the function

But that's what I don't understand! if I do that, if I say myWord = "" in my function, then what I am trying to do will be reset every time I run the function!

Also why the heck would I need to initialize it in there anyways, isn't the entire point that it's a global variable and it was created at the global level?



did you jump to python from java :D what about not using global variables? more than often they are an antipattern.
Age of Mythology forever!
Acrofales
Profile Joined August 2010
Spain18433 Posts
Last Edited: 2017-01-10 16:47:34
January 10 2017 16:45 GMT
#16508
result isn't global. It's in the enclosing scope. And to be more specific, your global variable result is defined when you say "global result", but you haven't initialized it anywhere.
Silvanel
Profile Blog Joined March 2003
Poland4769 Posts
Last Edited: 2017-01-10 16:50:05
January 10 2017 16:47 GMT
#16509
Yeah. Thats what i get:


myWord = ""

def myFunction():
global myWord
myWord = myWord + "more words"

myFunction()

print(myWord)


as expected it prints
more words
to terminal

And as pointed out in Your code "result" isnt global. It is local to "def recall_password" function.
Pathetic Greta hater.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2017-01-10 16:55:40
January 10 2017 16:53 GMT
#16510
ohhhhhhh right, god im dumb

thanks guys

i forgot that the entire block of code was put in the website's function (def recall_password(cipher_grille, ciphered_password)



On January 11 2017 01:45 mantequilla wrote:
+ Show Spoiler +
On January 11 2017 00:52 travis wrote:
I am so confused about global variables. I am told this should be intuitive but I don't understand how to do something.

I want a global variable, let's call it myWord.

I want a function that modifies myWord. let's call it myFunction

so my python file could look like


myWord = ""

def myFunction():
global myWord
myWord = myWord + "more words"


this is basically what I am trying to do.
But python doesn't like this. it tells me I need to define(initialize?) the global variable within the function

But that's what I don't understand! if I do that, if I say myWord = "" in my function, then what I am trying to do will be reset every time I run the function!

Also why the heck would I need to initialize it in there anyways, isn't the entire point that it's a global variable and it was created at the global level?



did you jump to python from java :D what about not using global variables? more than often they are an antipattern.


I don't know how to design my program in a way where I don't have to use global variables
phar
Profile Joined August 2011
United States1080 Posts
January 10 2017 17:09 GMT
#16511
Return more things is one quick and easy way generally(which in Python is pretty easy).
Who after all is today speaking about the destruction of the Armenians?
Manit0u
Profile Blog Joined August 2004
Poland17842 Posts
January 10 2017 19:19 GMT
#16512
On January 11 2017 02:09 phar wrote:
Return more things is one quick and easy way generally(which in Python is pretty easy).


Yeah, the ability to return more than one thing from a Python function has always amazed and baffled me at the same time.
Time is precious. Waste it wisely.
RoomOfMush
Profile Joined March 2015
1296 Posts
January 10 2017 20:10 GMT
#16513
On January 11 2017 04:19 Manit0u wrote:
Show nested quote +
On January 11 2017 02:09 phar wrote:
Return more things is one quick and easy way generally(which in Python is pretty easy).


Yeah, the ability to return more than one thing from a Python function has always amazed and baffled me at the same time.

How?
mantequilla
Profile Blog Joined June 2012
Turkey781 Posts
January 10 2017 20:44 GMT
#16514
apparently it's one of the many syntactic sugars of python


>>> def foo():
... return 1, 2, 3, 4 # Returns a tuple
>>> foo()
(1, 2, 3, 4)

>>> a, b, c, d = foo()
>>> a
1
>>> b
2
>>> c
3
>>> d
4


not my cup of tea though. Better return a well defined object
Age of Mythology forever!
RoomOfMush
Profile Joined March 2015
1296 Posts
January 10 2017 20:50 GMT
#16515
I meant: How is that amazing and baffling?
Returning one object is just as good as returning many. If worst comes to worst return an array of objects. Not a clean solution by any definition but 100% functional.
Silvanel
Profile Blog Joined March 2003
Poland4769 Posts
January 11 2017 08:06 GMT
#16516
Since i know only python i have to ask. Which langauges limit function to returning one item?
Pathetic Greta hater.
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
January 11 2017 08:43 GMT
#16517
On January 11 2017 17:06 Silvanel wrote:
Since i know only python i have to ask. Which langauges limit function to returning one item?

Most popular languages only have single return values, as far as I know. That single value still can be a tuple or (reference/pointer to) anything you want though.
If you have a good reason to disagree with the above, please tell me. Thank you.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
January 11 2017 08:48 GMT
#16518
On January 11 2017 17:06 Silvanel wrote:
Since i know only python i have to ask. Which langauges limit function to returning one item?


This whole discussion is confused because without defining what an "item" is it's meaningless.


>>> def test():
... return 1,2
...
>>> test()
(1, 2)
>>> type(_)
<class 'tuple'>


You're still returning one item, the item just happens to be a container. Python has some syntactic sugar around creating tuples and destructing tuples, such that if you have a collection of N items on rhs of assignment, you can assign it to [x0..xN) variables on the left hand side. Other languages have more advanced pattern matching, e.g. any trivial haskell example that matches (x:xs) on a list.

You can work around the lack of tuples in Java with a tuple object, but you can't do the variable destructing on the return value such that you assign multiple variables to the items of your collection in one line.

Go is a language where you can really return multiple values as a special quirk of the language, where you're not returning a tuple. They trade off making multiple returns a special case for saving complexity of implementing tuples + maybe language designers just don't like them.
RIP GOMTV. RIP PROLEAGUE.
Manit0u
Profile Blog Joined August 2004
Poland17842 Posts
January 11 2017 09:46 GMT
#16519
I have a bit of a problem with the DB:


A:
has one B
belongs to C

B:
belongs to A
has many C

C:
belongs to B


How do I set my cascades properly so that when I delete A all of B and C are also removed? Right now I'm getting an error that C can't be deleted because it's still referenced by A...
Time is precious. Waste it wisely.
Acrofales
Profile Joined August 2010
Spain18433 Posts
January 11 2017 09:55 GMT
#16520
On January 11 2017 18:46 Manit0u wrote:
I have a bit of a problem with the DB:


A:
has one B
belongs to C

B:
belongs to A
has many C

C:
belongs to B


How do I set my cascades properly so that when I delete A all of B and C are also removed? Right now I'm getting an error that C can't be deleted because it's still referenced by A...

Looks like you created a deadlock situation. Change the restrictions on your DB, or accept that there will be undeletable entries. Your only other option is a hack where you add a C called "tobedeleted" or something, and change A to belong to that first, then cascade your delete.
Prev 1 824 825 826 827 828 1032 Next
Please log in or register to reply.
Live Events Refresh
Kung Fu Cup
11:00
2026 Week 22
IntoTheiNu 1520
WardiTV633
RotterdaM462
IndyStarCraft 193
TKL 158
Rex139
SteadfastSC97
EnkiAlexander 16
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 462
IndyStarCraft 193
TKL 158
Rex 139
ProTech125
SteadfastSC 97
Trap 9
StarCraft: Brood War
Bisu 5837
Jaedong 4085
Soulkey 2860
Shuttle 1161
Horang2 999
Mini 752
Light 466
Hyuk 463
Snow 426
EffOrt 361
[ Show more ]
BeSt 330
Stork 208
firebathero 173
Last 161
Pusan 87
Barracks 74
hero 58
Hm[arnc] 50
Sharp 49
sSak 41
Mind 41
Yoon 36
PianO 35
ToSsGirL 30
Bale 23
soO 22
910 18
Terrorterran 17
Sacsri 12
SilentControl 10
scan(afreeca) 10
ajuk12(nOOB) 9
Icarus 9
Rock 9
Dota 2
qojqva2444
Dendi1855
syndereN657
LuMiX1
Counter-Strike
olofmeister1328
Super Smash Bros
hungrybox1126
Other Games
singsing2058
B2W.Neo654
Lowko408
crisheroes311
DeMusliM265
XaKoH 185
Liquid`VortiX141
Sick122
ArmadaUGS69
QueenE42
[ Show 12 non-featured ]
StarCraft 2
• intothetv
• AfreecaTV YouTube
• Kozan
• IndyKCrew
• Migwel
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• C_a_k_e 1921
• WagamamaTV709
League of Legends
• Jankos2241
• TFBlade850
Upcoming Events
Replay Cast
10h 21m
The PondCast
20h 21m
KCM Race Survival
20h 21m
OSC
1d 8h
Escore
1d 20h
IntoTheTV X SOOP
1d 21h
CranKy Ducklings
2 days
Sparkling Tuna Cup
3 days
Shopify Rebellion Sundays
4 days
Spirit vs Mixu
Clem vs TBD
RSL Revival
4 days
Serral vs Rogue
[ Show More ]
BlizzCon
4 days
Replay Cast
4 days
Afreeca Starleague
4 days
WardiTV Weekly
4 days
Afreeca Starleague
5 days
GSL
5 days
PiGosaur Cup
6 days
The PondCast
6 days
Kung Fu Cup
6 days
Liquipedia Results

Completed

Proleague 2026-09-08
PiG Sty Festival 8.0
Big Dog Cup 2026 Div 1

Ongoing

KCM Race Survival 2026 Season 3
K-JUNGMAN
ASL Season 22
Super Anchor Qualifying S3
CSL 2026 AUTUMN (S22)
Acropolis #5
Acropolis #5 - TRS
RSL Revival: Season 6
Calamity Invitational
FISSURE Playground #3
BLAST Open Fall 2026
Esports World Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026

Upcoming

Escore Tournament S3: King of Kings
Acropolis #5 - GSA
Blizzard Classic Cup 2026
Acropolis #5 - GSB
Acropolis #5 - GSC
SC4ALL II: Brood War
HSC XXX
Stellar Fest 2: Lunar Cup
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
RSL Offline Finals
Blizzard Classic Cup 2026
Stake Ranked Episode 6
BLAST Rivals Fall 2026
IEM Beijing 2026
Stake Ranked Episode 5
PGL Masters Bucharest 2026
1win Private Club #2
Thunderpick World Champ. '26
ESL Pro League Season 24
Stake Ranked Episode 4
1win Private Club #1
Logitech G Play Connect 2026
SL StarSeries Fall 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.