Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

#Python ((削除) 337 (削除ここまで) 336 chars)

Python ((削除) 337 (削除ここまで) 336 chars)

import re
i=input().split()
a,d,o,e=[],{},"",len(i)-1
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b)))
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a]
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] 
 if"H"not in b:d["-"]=0
d["H"]-=2*e;d["O"]-=e
for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else""
print(o)

Here is an example of how to use this program:

sh3.14$ python dehydrate.py <-Run the program
C6H12O6 HgHO4 <-Input
O9H11C6Hg <-Output

Try it!

#Python ((削除) 337 (削除ここまで) 336 chars)

import re
i=input().split()
a,d,o,e=[],{},"",len(i)-1
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b)))
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a]
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] 
 if"H"not in b:d["-"]=0
d["H"]-=2*e;d["O"]-=e
for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else""
print(o)

Here is an example of how to use this program:

sh3.14$ python dehydrate.py <-Run the program
C6H12O6 HgHO4 <-Input
O9H11C6Hg <-Output

Try it!

Python ((削除) 337 (削除ここまで) 336 chars)

import re
i=input().split()
a,d,o,e=[],{},"",len(i)-1
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b)))
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a]
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] 
 if"H"not in b:d["-"]=0
d["H"]-=2*e;d["O"]-=e
for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else""
print(o)

Here is an example of how to use this program:

sh3.14$ python dehydrate.py <-Run the program
C6H12O6 HgHO4 <-Input
O9H11C6Hg <-Output

Try it!

1 char improvement, changed example
Source Link
K Zhang
  • 7.3k
  • 1
  • 11
  • 13

#Python (337(削除) 337 (削除ここまで) 336 chars)

import re
def si=input(i):.split()
a,d,o,e=[],{},"",len(i)-1
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b)))
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a]
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] 
 if"H"not in b:d["-"]=0
d["H"]-=2*e;d["O"]-=e
for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else""
 return print(o)

The function s(i) takes an array of strings as an argument and outputs the result. HereHere is an example of ahow to use this program:

import re
def s(i):
 a,d,o,e=[],{},"",len(i)-1 #vars b and c are used for iterating, and are not defined here
 for b in i:a.append(dict(resh3.findall(r"([A-Z][a-z]*)(\d*)",b))) #convert each molecule from string to a dictionary and store into a
 a=[{b:int(c[b])if c[b]else 1for b in c}for c in a] #convert all "subscripts" in a to ints, making empty strings into 1s
 for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] #add key/value pairs of array of dictionaries a to dictionary d
 14$ if"H"notpython indehydrate.py b:d["<-"]=0 #check that all molecules have at leastRun onethe H(ydrogen)program
 d["H"]-=2*e;d["O"]-=e C6H12O6 HgHO4 #remove H(ydrogen) and O(xygen)
 for b in d:o+=b+(str(d[b])if d[b]<-1else"")if d[b]else"" #convert dictionary d into output string o
 return o #return o
inmol = ["C6H12O6","C6H12O6","C6H12O6","C6H12O6","C6H12O6","C6H12O6"]Input
print(s(inmol)) O9H11C6Hg #print output<-Output

Try it!

#Python (337 chars)

import re
def s(i):
a,d,o,e=[],{},"",len(i)-1
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b)))
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a]
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] 
 if"H"not in b:d["-"]=0
d["H"]-=2*e;d["O"]-=e
for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else""
 return o

The function s(i) takes an array of strings as an argument and outputs the result. Here is an example of a program:

import re
def s(i):
 a,d,o,e=[],{},"",len(i)-1 #vars b and c are used for iterating, and are not defined here
 for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b))) #convert each molecule from string to a dictionary and store into a
 a=[{b:int(c[b])if c[b]else 1for b in c}for c in a] #convert all "subscripts" in a to ints, making empty strings into 1s
 for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] #add key/value pairs of array of dictionaries a to dictionary d
  if"H"not in b:d["-"]=0 #check that all molecules have at least one H(ydrogen)
 d["H"]-=2*e;d["O"]-=e  #remove H(ydrogen) and O(xygen)
 for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else"" #convert dictionary d into output string o
 return o #return o
inmol = ["C6H12O6","C6H12O6","C6H12O6","C6H12O6","C6H12O6","C6H12O6"]
print(s(inmol))  #print output

#Python ((削除) 337 (削除ここまで) 336 chars)

import re
i=input().split()
a,d,o,e=[],{},"",len(i)-1
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b)))
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a]
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] 
 if"H"not in b:d["-"]=0
d["H"]-=2*e;d["O"]-=e
for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else""
print(o)

Here is an example of how to use this program:

sh3.14$ python dehydrate.py <-Run the program
C6H12O6 HgHO4 <-Input
O9H11C6Hg <-Output

Try it!

Changed program to be a function
Source Link
K Zhang
  • 7.3k
  • 1
  • 11
  • 13

#Python ((削除) 312 (削除ここまで)(削除) 309 (削除ここまで) 308337 chars)

import re
def s(i):
a,d,o,e=[],{},"",len(i)-1
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b)))
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a]
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] 
 if"H"not in b:d["-"]=0
d["H"]-=2*e
d["O"]=2*e;d["O"]-=e
for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else""
 return o

The input for this piece of code is the array of stringsfunction s(i), takes an array of strings as an argument and outputs the output is oresult. Here Here is an example of the code with input, output, and comments added ina program:

import re
i=["HgHO4","C6H12O6"] #input, as an array ofdef stringss(i):
a,d,o,e=[],{},"",len(i)-1 #vars b and c are used for iterating, and are not defined here
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b))) #convert each molecule from string to a dictionary and store into a
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a] #convert all "subscripts" in a to ints, making empty strings into 1s
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] #add key/value pairs of array of dictionaries a to dictionary d
 if"H"not in b:d["-"]=0 #check that all molecules have at least one H(ydrogen)
d["H"]-=2*e =2*e;d["O"]-=e #remove H(ydrogen) and O(xygen)
 for b #removein Hd:o+=b+(ydrogenstr(d[b])
d["O"]-=eif d[b]-1else"")if d[b]else"" #convert dictionary d into output string o
 return o #remove O(xygen)
for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else"" #convert dictionary d into output string#return o
inmol = ["C6H12O6","C6H12O6","C6H12O6","C6H12O6","C6H12O6","C6H12O6"]
print(os(inmol)) #print output

#Python ((削除) 312 (削除ここまで)(削除) 309 (削除ここまで) 308 chars)

import re
a,d,o,e=[],{},"",len(i)-1
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b)))
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a]
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] 
 if"H"not in b:d["-"]=0
d["H"]-=2*e
d["O"]-=e
for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else""

The input for this piece of code is the array of strings i, and the output is o. Here is an example of the code with input, output, and comments added in:

import re
i=["HgHO4","C6H12O6"] #input, as an array of strings
a,d,o,e=[],{},"",len(i)-1 #vars b and c are used for iterating, and are not defined here
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b))) #convert each molecule from string to a dictionary and store into a
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a] #convert all "subscripts" in a to ints, making empty strings into 1s
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] #add key/value pairs of array of dictionaries a to dictionary d
 if"H"not in b:d["-"]=0 #check that all molecules have at least one H(ydrogen)
d["H"]-=2*e  #remove H(ydrogen)
d["O"]-=e #remove O(xygen)
for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else"" #convert dictionary d into output string o
print(o) #print output

#Python (337 chars)

import re
def s(i):
a,d,o,e=[],{},"",len(i)-1
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b)))
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a]
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] 
 if"H"not in b:d["-"]=0
d["H"]-=2*e;d["O"]-=e
for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else""
 return o

The function s(i) takes an array of strings as an argument and outputs the result. Here is an example of a program:

import re
def s(i):
a,d,o,e=[],{},"",len(i)-1 #vars b and c are used for iterating, and are not defined here
for b in i:a.append(dict(re.findall(r"([A-Z][a-z]*)(\d*)",b))) #convert each molecule from string to a dictionary and store into a
a=[{b:int(c[b])if c[b]else 1for b in c}for c in a] #convert all "subscripts" in a to ints, making empty strings into 1s
for b in a:
 for c in b:d[c]=c in d and d[c]+b[c]or b[c] #add key/value pairs of array of dictionaries a to dictionary d
 if"H"not in b:d["-"]=0 #check that all molecules have at least one H(ydrogen)
d["H"]-=2*e;d["O"]-=e #remove H(ydrogen) and O(xygen)
 for b in d:o+=b+(str(d[b])if d[b]-1else"")if d[b]else"" #convert dictionary d into output string o
 return o #return o
inmol = ["C6H12O6","C6H12O6","C6H12O6","C6H12O6","C6H12O6","C6H12O6"]
print(s(inmol)) #print output
1 char improvement
Source Link
K Zhang
  • 7.3k
  • 1
  • 11
  • 13
Loading
Shortened program
Source Link
K Zhang
  • 7.3k
  • 1
  • 11
  • 13
Loading
Source Link
K Zhang
  • 7.3k
  • 1
  • 11
  • 13
Loading

AltStyle によって変換されたページ (->オリジナル) /