eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm trying to understand why the following solution for LeetCode's Single Number II works in Python: class Solution: def singleNumber(self, nums: List[int]) -> int: number = 0 for i in range(32): count = 0 for num in nums: if num & (1 << i): count += 1 if count % 3: number |= (1 << i) if number & (1 << 31): number -= (1 << 32) return number  But I'm confused about a few things: In Python, integers are arbitrary precision, so they're not stored in 32-bit like in C or C++. sys.getsizeof() even shows 28 bytes for small integers. So how can we assume that the number fits in 32 bits and that bit 1 << 31 is actually the sign bit? Why do we loop only from i in range(32)? Why not less If I input small integers (like 2, 3, etc.), they don't "fill" 32 bits — so how can checking the 31st bit be reliable? Basically, since Python ints grow as needed and aren’t stored in 32-bit containers, how does this approach still work correctly when simulating 32-bit signed behavior? I tried understanding similar questions and answers (like on CodeReview.SE), and I get the general idea — Python ints are arbitrary precision and we simulate 32-bit behavior using bitmasking and shifting. But I'm still confused why this actually works reliably in Python. My Questions: Why can we safely assume a 32-bit simulation works in Python? Why is checking the 31st bit (1 << 31) meaningful in Python? Why doesn’t the arbitrary-size nature of Python integers break this logic?

Answer: Think of positive ints having infinitely many zero-bits in front and of negative ints having infinitely many one-bits in front:  3 = ...00000011 2 = ...00000010 1 = ...00000001 0 = ...00000000 -1 = ...11111111 -2 = ...11111110 -3 = ...11111101 -4 = ...11111100  That's how Python treats them, which is why for example (-3) & (1<<1000) is 21000. LeetCode's problem has numbers in the range -2^31 to 2^31-1. Let's say the 31 were 2 instead. Then the above -4 to 3 would be all possible values. You can see that for each number, all but the last two bits are always the same. Either they're all 0, or they're all 1. So it suffices to just find out the answer number's last three bits and duplicate the third-to-last bit to the infinite bits before. Let's say the correct answer for some input were -3. Then we'd first find that the last three bits are 101. Which represents the number 5. Then we check whether the third-to-last is 1. Since it is, we subtract 23=8 and end up with 5-8 = -3. With LeetCode's limit using 31, it likewise suffices to find out the answer number's last 32 bits (that's what the for i in range(32): block does) and duplicate the 32nd-to-last bit to the infinite bits before (that's what the if number & (1 << 31): number -= (1 << 32) does).
Question: I'm using polars with a data frame whose schema looks like this: Schema({'CustomerID': String, 'StockCode': String, 'Total': Int64})  interpreted as "Customer CustomerID bought Total of product StockCode." I'm looking for an efficient way to generate all unique pairs of customer IDs such that the two customers purchased at least one of the same product. So, given: import polars as pl df = pl.from_repr(""" ┌─────────────┬────────────┬───────┐ │ CustomerID ┆ StockCode ┆ Total │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ f64 │ ╞═════════════╪════════════╪═══════╡ │ A ┆ 123 ┆ 45.78 │ │ A ┆ 140 ┆ 10.26 │ │ B ┆ 125 ┆ 99.62 │ │ B ┆ 128 ┆ 23.65 │ │ B ┆ 140 ┆ 92.95 │ │ C ┆ 123 ┆ 45.78 │ │ D ┆ 145 ┆ 7.58 │ └─────────────┴────────────┴───────┘ """)  the algorithm should produce: [ ( 'A', 'B' ), # Because of product 140 ( 'A', 'C' ) # Because of product 123 ]  I'll then use this to compute a pairwise similarity measure (e.g. dot product or cosine) and then, as efficiently as possible, produce a data frame that looks like: Schema({'ID1': String, 'ID2': String, 'Similarity': Float64})  likely with a threshold for minimum similarity. Doing something like:  from itertools import combinations row_keys = pl.Series( df.select(category_col) .unique( ).drop_nulls( ).collect() ).to_list() pairs = combinations(row_keys, 2)  generates all possible pairs, but is hugely inefficient.

Answer: Use a self-join: (df.lazy() .join_where(df.lazy(), ((pl.col.StockCode == pl.col.StockCode_right) & (pl.col.CustomerID < pl.col.CustomerID_right))) .select("^CustomerID.*$") .unique() .collect(engine="streaming"))  ┌────────────┬──────────────────┐ │ CustomerID ┆ CustomerID_right │ │ --- ┆ --- │ │ str ┆ str │ ╞════════════╪══════════════════╡ │ A ┆ C │ │ A ┆ B │ └────────────┴──────────────────┘
Question: I need to clean few xml files from not needed elements, came up with this wild card replacement for nothing to get rid of not needed <EmbeddedImage>, but I'm missing how to store each change in the loop, looks like it overwrites it each time. Thought that Python will keep it, but it's not the case. Do I need to create some additional logic to get only single Brand_1 in my final tree? Appreciate if you point me how I can fix it. import re from bs4 import BeautifulSoup as bs filepath = "C://ddd/test.xml" xml = ''' <Images> <EmbeddedImage Name="Brand_1"> <ImageData>/9j/4AAQSkZJB//2Q==</ImageData> </EmbeddedImage> <EmbeddedImage Name="Brand__2XX"> <ImageData>/9j/4AAQSkZJB//2Q==JB//2</ImageData> </EmbeddedImage> <EmbeddedImage Name="Brand___3XX"> <ImageData>/9j/4AAQSkZAAQSkkB//2Q=AAQSk=</ImageData> </EmbeddedImage> <Images>''' Bs_data = bs(xml,'xml') #contant = xml ?? content = xml for item in Bs_data.select ('EmbeddedImage'): #print('Name= ',item.get('Name')) if item.get('Name') != 'Brand_1': wild = '<EmbeddedImage Name="'+item.get('Name')+'">.*</EmbeddedImage>' ##<@><< first </ How??? content = re.sub(wild, '', content , flags=re.DOTALL) print ('------------',wild,'::::',content) print(content) # only Brand_1 

Answer: Try to operate directly with Bs.data from bs4 import BeautifulSoup as bs xml = ''' <Images> <EmbeddedImage Name="Brand_1"> <ImageData>/9j/4AAQSkZJB//2Q==</ImageData> </EmbeddedImage> <EmbeddedImage Name="Brand__2XX"> <ImageData>/9j/4AAQSkZJB//2Q==JB//2</ImageData> </EmbeddedImage> <EmbeddedImage Name="Brand___3XX"> <ImageData>/9j/4AAQSkZAAQSkkB//2Q=AAQSk=</ImageData> </EmbeddedImage> </Images> ''' soup = bs(xml, 'xml') for item in soup.find_all('EmbeddedImage'): if item.get('Name') != 'Brand_1': item.decompose() print(soup.prettify())
Question: Python does not allow overloading. I therefore expect it to only allow a single class method with the same name. This is not the case. This code runs without issue. class DoubleTest: def __init__(self): self._generate_thing() def _generate_thing(self, entry): print(entry) def _generate_thing(self): print("do the thing")  This code produces a TypeError about the wrong number of supplied parameters. class DoubleTest: def __init__(self): self._generate_thing("test") def _generate_thing(self, entry): print(entry) def _generate_thing(self): print("do the thing")  Calling dir(test) shows me that there is only 1 _generate_thing method. But why am I allowed to define it twice and how did it choose which one to use?

Answer: Python doesn't allow multiple methods with the same name, but it does allow you to redefine methods and functions. As a result, only the last method with the duplicate name is effective, the other one is effectively ignored because it has been replaced by the time the class definition is completed.
Question: I am trying to sort a list of 3D coordinates. The target is to get the same order that the basic Python list sort produces, which is the same as the meshgrid + column_stack order. Python sorts by the first value of each element first; in case of ties, the second element is checked, and so on. I would like to do the same in numpy, especially with the argsort function, as it gives an index list for the sorted order. I need this list to sort other numpy arrays with the same first dimension. I think this post is related, but does not use numpy to do the sorting. Following a small test bench. import numpy as np num = 10 x = np.linspace(1, 3, num) y = np.linspace(4, 6, num) z = np.linspace(7, 9, num) xg, yg , zg = np.meshgrid(x, y, z, indexing='ij', sparse=False) test = np.column_stack((xg.ravel(), yg.ravel(), zg.ravel())) print('Target ordering', test) np.random.shuffle(test) print('\nShuffeld', test) python_sorted = sorted(test.tolist()) python_sorted = np.array(python_sorted) print('\nPython list sorted', python_sorted) numpy_sort_idx = test.argsort(axis=0) numpy_sorted = test[numpy_sort_idx] print('\nNumpy arg sorted', numpy_sorted) 

Answer: You could use lexsort on the reversed columns: import numpy as np num = 10 x = np.linspace(1, 3, num) y = np.linspace(4, 6, num) z = np.linspace(7, 9, num) xg, yg , zg = np.meshgrid(x, y, z, indexing='ij', sparse=False) test = np.column_stack((xg.ravel(), yg.ravel(), zg.ravel())) print('Target ordering\n', test) np.random.shuffle(test) print('\nShuffled\n', test) python_sorted = sorted(test.tolist()) python_sorted = np.array(python_sorted) print('\nPython list sorted\n', python_sorted) idx = np.lexsort(test[:, ::-1].T) numpy_sorted = test[idx] print('\nLexsorted using reversed columns\n', numpy_sorted)  The order of the columns is reversed due to the precedence of the keys passed to lexsort. As the first column should be the primary sort order, it must come last. From the documentation:  The last key in the sequence is used for the primary sort order, ties are broken by the second-to-last key, and so on.  Output form the test bench Target ordering [[1. 4. 7. ] [1. 4. 7.22222222] [1. 4. 7.44444444] ... [3. 6. 8.55555556] [3. 6. 8.77777778] [3. 6. 9. ]] Shuffled [[3. 5.11111111 7.22222222] [2.77777778 5.11111111 8.11111111] [1.66666667 5.77777778 7.22222222] ... [2.55555556 4.88888889 7.22222222] [2.55555556 4.88888889 7.66666667] [2.55555556 4.66666667 7.66666667]] Python list sorted [[1. 4. 7. ] [1. 4. 7.22222222] [1. 4. 7.44444444] ... [3. 6. 8.55555556] [3. 6. 8.77777778] [3. 6. 9. ]] Lexsorted using reversed columns [[1. 4. 7. ] [1. 4. 7.22222222] [1. 4. 7.44444444] ... [3. 6. 8.55555556] [3. 6. 8.77777778] [3. 6. 9. ]]
Question: I have a dataframe as such: Starting point Walking Driving Lunch 0 8 4 Shopping 0 7 3 Coffee 0 5 2  Where I want to draw, for each index, a green line from "Starting point" -> "Walking", and a red line from "Starting point" -> "Coffee". To do this, I loop through both the columns and the index, as such: for column in df7.columns: for idx in df7.index: fig9.add_trace( go.Scatter( # chart # chart # chart ) )  Which gives me the following chart of two lines, colored by cycle, overlapping.  The question is: how can I modify the code in plotly python to create jitter in the y axis for the lines of different cycles? In other words, the shorter line will be above the longer line. Full MRE: df7 = pd.DataFrame({ 'Starting point': [0, 0, 0], 'Walking': [8, 7, 5], 'Biking': [4, 3, 2] }, index=['Lunch', 'Shopping', 'Coffee']) fig9 = go.Figure() color_cyc = cycle(["#888888", "#E2062B"]) symbol_cyc = cycle(["diamond", "cross"]) for column in df7.columns: color=next(color_cyc) for idx in df7.index: fig9.add_trace( go.Scatter( y=[idx] * len(df7.loc[idx, ["Starting point", column]]), x=df7.loc[idx, ["Starting point", column]], showlegend=False, mode="lines+markers", marker={ "color": color, "symbol": "diamond", # "jitter": 0.4, }, ), ) fig9  Thanks very much.

Answer: Try to use offset and then enumerate import pandas as pd import plotly.graph_objects as go from itertools import cycle df7 = pd.DataFrame({ 'Starting point': [0, 0, 0], 'Walking': [8, 7, 5], 'Biking': [4, 3, 2] }, index=['Lunch', 'Shopping', 'Coffee']) fig9 = go.Figure() color_cyc = cycle(["#888888", "#E2062B"]) symbol_cyc = cycle(["diamond", "cross"]) col_offsets = {'Walking': 0.15, 'Biking': -0.15} for column in ['Walking', 'Biking']: color = next(color_cyc) offset = col_offsets[column] for i, idx in enumerate(df7.index): y_val = i + offset fig9.add_trace( go.Scatter( y=[y_val, y_val], x=[df7.loc[idx, "Starting point"], df7.loc[idx, column]], showlegend=False, mode="lines+markers", marker=dict( color=color, symbol="diamond" ), line=dict(color=color) ) ) fig9.update_yaxes( tickvals=list(range(len(df7.index))), ticktext=list(df7.index) ) fig9.show()  Output:
Question: I have a window signal, which i calculate it's Fourier coefficients but then in the output i get a small complex value [3 order of magnitude less in the origin and the same order of magnitude in the edges of my sampling (from -1000 to 1000)] where the output should be purely real, if it was just a floating point approx error it wouldn't have the pattern of sin like i found, so what could cause this? It's all done in Python (through Google Colab) with the libraries cmath, numpy, and matplotlib alone. import numpy as np import cmath import matplotlib.pyplot as plt D=1000 a=np.zeros(2*D+1) for i in range(-100,100): a[i+D] = 1  This code creates the following window signal:  and then to get the fourier coefficients: j=complex(0,1) pi=np.pi N=2001 ak = np.zeros(N, dtype=complex) for k in range(-D, D + 1): for n in range(-D, D + 1): ak[k + D] += (1/N) * a[n + D] * cmath.exp(-j * 2 * pi * k * n / N)  which gives me the following image:  when i do the math in paper i get the coeficients to be:  where omega0 is 2*pi/N and a[n]=u(n+100)-u(n-100) (where u(n) is the Heaveside function). but it's weird as when i plot the imaginary part of ak i get that it's (at a very good approximation) equal to 1/N *sin(201*pi/N where N=2001 and is my entire sampling area (from -1000 to 1000 as i mentioned), which if it was just a rounding error because of floating point it wouldn't be in this form. here it's the plot of the imaginary part of ak, not freq of ak or bk. 

Answer: the imaginary part you see is correct, your textbook definition speaks of a signal symmetric around 0, your signal is only from -100 to 99 as python range is end exclusive, it is not symmetric around 0, hence there's a phase error, which translates to an imaginary component in the signal Fourier transform. correcting the code: for i in range(-100,101): # from -100 to 100 a[i+D] = 1  gives for the imaginary component  numpy double precision has around 16 digits of precision, your real part is in order of 1e-2 the imaginary part being in order of 1e-18 is close to machine precision limits.
Question: I'm using VS Code with Pylance, and having problems correctly writing type hints for the following function. To me, the semantics seem clear, but Pylance disagrees. How can I fix this without resorting to cast() or # type ignore? Pylance warnings are given in the comments. I am fully committed to typing in Python, but it is often a struggle. T = TypeVar("T") def listify(item: T | list[T] | tuple[T, ...] | None) -> list[T]: if item is None: return [] elif isinstance(item, list): return item # Return type, "List[Unknown]* | List[T@listify]", # is partially unknown elif isinstance(item, tuple): return list(item) # Return type, "List[Unknown]* | List[T@listify]", # is partially unknown else: return [item] 

Answer: This is just fundamentally unsafe, due to ambiguity. Suppose you have a generic function that uses listify: def caller[T](x: T): y = listify(x)  What's the proper annotation for y? The obvious answer is list[T]. But there's no guarantee listify will return a list[T]. If T is list[Something], or tuple[Something, ...], or None, listify can return something completely different.  There is no way to make a fully generic listify safe. Pylance does a stricter job than mypy of reporting this kind of ambiguity, but the fundamental ambiguity is there no matter what type checker you use. And even if you completely strip out the implementation, and simplify it down to def f[T](x: T | list[T]) -> list[T]: raise NotImplementedError reveal_type(f([3]))  mypy will report the following: main.py:4: note: Revealed type is "builtins.list[Never]" main.py:4: error: Argument 1 to "f" has incompatible type "list[int]"; expected "list[Never]" [arg-type] Found 1 error in 1 file (checked 1 source file)  because the ambiguity is causing it to deduce conflicting type requirements.
Question: Imagine I have a dataframe like so: ID, place, stock 1, 1, 4 2, NaN, 2 3, NaN, NaN 4, 1, 1  I wish to find all the rows for which place is null and check the corresponding stock column to see, for instance, if place is Null but stock is >0. I've implemented this by iterating over the dataframe like so: for idx, x in enumerate(df[place].isnull().tolist()): ...  But I am sure there is a simpler way to do this. For example, for the above data for each row for which place is null, I wish to check if the corresponding stock column is >0 and count how many occurrences are like so. In actual fact, I wish to check multiple columns (>10) to see if they meet certain criteria, and so am more interested in a generic solution to this.

Answer: You can use something vectorized like that count = df[df['place'].isnull() & (df['stock'] > 0)].shape[0]
Question: I try to parse XML file to get NeedThisValue!!! for one of the element tagged <Value>. But there are several tags <Value> in file. How I can get the right one under <Image> branch? This is example of my XML: <Report xmlns=http://schemas.microsoft.com> <AutoRefresh>0</AutoRefresh> <DataSources> <DataSource Name="DataSource2"> <Value>SourceAlpha</Value> <rd:SecurityType>None</rd:SecurityType> </DataSource> </DataSources> <Image Name="Image36"> <Source>Embedded</Source> <Value>NeedThisValue!!!</Value> <Sizing>FitProportional</Sizing> </Image> </Report>  And I'm using this code: from bs4 import BeautifulSoup with open(filepath, 'r') as f: data = f.read() Bs_data = BeautifulSoup(data, "xml") b_unique = Bs_data.find_all('Value') print(b_unique)  Result is below, I need second one only. [<Value>SourceAlpha</Value>, <Value>NeedThisValue!!!</Value>] 

Answer: As an alternative to the accepted solution from @Igel, you can reach it also with lxml and xpath(): from lxml import html broken_xml = """<Report xmlns=http://schemas.microsoft.com> <AutoRefresh>0</AutoRefresh> <DataSources> <DataSource Name="DataSource2"> <Value>SourceAlpha</Value> <rd:SecurityType>None</rd:SecurityType> </DataSource> </DataSources> <Image Name="Image36"> <Source>Embedded</Source> <Value>NeedThisValue!!!</Value> <Sizing>FitProportional</Sizing> </Image> </Report> """ tree = html.fromstring(broken_xml) print(html.tostring(tree, pretty_print=True).decode()) value_elem = tree.xpath('//image[@name="Image36"]/value')[0] print(value_elem.text)  Output: <report xmlns="http://schemas.microsoft.com"> <autorefresh>0</autorefresh> <datasources> <datasource name="DataSource2"> <value>SourceAlpha</value> <securitytype>None</securitytype> </datasource> </datasources> <image name="Image36"> <source>Embedded</source> <value>NeedThisValue!!!</value> <sizing>FitProportional</sizing> </image> </report> NeedThisValue!!!
Question: I am studying numerical analysis and I have come across this dilemma. Running the following script, from decimal import Decimal a = 0.1 ; N = 100000 ; # product calculation P = N*a # Print product result with no apparent error print(' %.22f ' % P) # Print product result with full Decimal approximation of 0.1 print(Decimal(0.1) * 100000)  I realize that despite 0.1 not having an accurate floating-point representation, when I multiply it by 100000 (which has an exact floating-point representation), and increase the precision of how I print the result, I do not notice any error. print(' %.22f ' % P) # Result: 10000.0000000000000000000000  This is in contrast to the case where I use the Decimal method, where I can see the error behind the product. print(Decimal(0.1) * 100000)  Also, how come I can print up to 55th digits of precision of a number if the IEEE754 standard only allows 53? I reproduced this case with the following instruction: print("%.55f" % 0.1) #0.1000000000000000055511151231257827021181583404541015625  Can anyone explain why this happens?

Answer:  a = 0.1 ;  Assuming your Python implementation uses IEEE-754 binary641, this converts 0.1 to 0.1000000000000000055511151231257827021181583404541015625, because that is the representable value that is nearest to 0.1.  P = N*a  The real-number arithmetic product of 100,000 and 0.1000000000000000055511151231257827021181583404541015625 is 10,000.00000000000055511151231257827021181583404541015625. This number is not representable in binary64. The two nearest representable values are 10,000 and 10000.000000000001818989403545856475830078125. The floating-point multiplication produces the representable value that is closer, so N*a produces 10,000.  print(' %.22f ' % P)  This prints the value stored in P, formatted with 22 digits after the decimal point, yielding “10000.0000000000000000000000”.  print(Decimal(0.1) * 100000)  In this, first 0.1 is converted to binary floating-point, yielding 0.1000000000000000055511151231257827021181583404541015625. Then Decimal(0.1) converts that number to Decimal, which produces the same value. Then the multiplication by 100,000 is performed. By default, Python uses only 28 digits for Decimal arithmetic, so the result of this multiplication is rounded to 10,000.00000000000055511151231. Footnote 1 This is common, but Python does not have a formal specification, and what documentation there is for it is weak about floating-point behavior.
Question: Problem:  You will be given three integers A, B and C. The numbers will not be given in that exact order, but we do know that A < B < C. In order to make for a more pleasant viewing, we want to rearrange them in the given order. The first line of the input contains three positive integers A, B and C, not necessarily in that order. All three numbers will be less than or equal to 100. The second line contains three uppercase letters 'A', 'B' and 'C' (with no spaces between them) representing the desired order. Output the A, B and C in the desired order on a single line, separated by single spaces.  Sample input 1: 1 5 3 ABC Sample output 1: 1 3 5 Sample input 2: 6 4 2 CAB Sample output 2: 6 2 4 This problem comes from an online judge system. There are 10 checkpoints. The following code passes only 2 of them, although the code works fine when I run it outside. l = list(map(int,input().split())) order = input() l.sort() if order == "ABC": print(l[0],l[1],l[2]) elif order == "ACB": print(l[0],l[2],l[1]) elif order == "BAC": print(l[1],l[0],l[2]) elif order == "BCA": print(l[1],l[2],l[0]) elif order == "CAB": print(l[2],l[0],l[1]) else: print(l[2],l[1],l[0])  And if I change the else: in the second last line to elif order == "CBA":, very surprisingly it fails all the checkpoints. The feedback/reason for the failure is Wrong Answer (as opposed to Runtime Error). Finally, my friend sent me this code which passes all the checkpoints: l = list(map(int,input().split())) order = input() l.sort() result = [] for i in order: if i == "A": result.append(l[0]) elif i == "B": result.append(l[1]) elif i == "C": result.append(l[2]) print(" ".join(map(str, result)))  I can see that my code is less neat, but I want to know why it fails so many checkpoints. Thank you in advance.

Answer: The order input apparently has extra whitespace around the letters, so it doesn't exactly equal any of the strings you're comparing with. Your friend's solution ignores any characters that aren't in ABC, so it ignores the extra spaces. You can remove this extraneous whitespace with order = input().strip()  I'd say this is a bug in the online judging system. Nothing in the problem specification mentions this whitespace, so there's no reason why the program should have to accomodate it. However, when you become a professional programmer it's often a good idea to anticipate inputs that don't match the specifications precisely. If you can handle it easily, it's a good idea. This is especially true if the input comes from interactive users, as they can't generally be trusted unless the UI performs validation.