基于范围的数字猜谜游戏(range based number guessing game)
我想找到一个范围内两个数字之间的差异,但我需要能够环绕到范围的开头,就像一个循环列表。
范围是9。
因此,如果数字为6且猜测为5,则答案应为1,但如果数字为8且猜测为2,则答案应为3。
我的第一个想法是将数字增加10,如下所示:
n is the correct number, g is the guess, r is the result. ( let [ r (min (- (+ n 10) g) (- g n)) ] (if (> 0 r) ( * -1 r ) r) ) )
...并且它适用于环绕,但问题是数字10的存在如果包装则将结果增加1。 只是从结果中减去1或数字在所有情况下都不起作用。
根据所讨论的数字,结果是否定的,因此if语句是将其交换为正数。
这不完全是一个clojure问题,它确实是一个数学问题,我在任何语言中都会遇到这个问题,但它恰好就是我正在编写的内容。我刚开始使用clojure(或任何函数)语言),所以我完全可能做错了或非常单一的。
谢谢你的帮助
I want to find the difference between two numbers in a range, but I need to be able to wrap around to the beginning of the range, like a circular list.
The range is 9.
So if the number is 6 and the guess is 5 the answer should be 1, but if the number is 8 and the guess is 2, then the answer should be 3.
My first thought was to bump the number by 10 like this:
n is the correct number, g is the guess, r is the result. ( let [ r (min (- (+ n 10) g) (- g n)) ] (if (> 0 r) ( * -1 r ) r) ) )
... and that worked for wrapping around, but then the problem is that the existence of the number 10 increases the result by 1 if it wraps. Just subtracting 1 from the result or the number doesn't work either in all cases.
Depending on the numbers in question, the result is negative, so the if statement is to swap it around to positive.
This isn't a clojure problem exactly, it's really a math issue and I'd have this problem in any language, but it so happens that's what I'm writing it in. I've only just started using clojure (or any functional language), so it's entirely possibly I'm doing things wrong or wildly unidiomatic.
thanks for any help
原文:https://stackoverflow.com/questions/13275704
最满意答案
您可以使用
div()
方法(与divide()
相同,参数axis
设置为0,以沿行元素划分执行:df.div(df['0'], axis = 0) #interval 0 48 #field well #Field A 0 1.0 0.500000 # 1 1.0 0.200000 # 2 1.0 0.100000 #Field B 0 1.0 0.500000 # 1 1.0 0.333333 # 2 1.0 0.166667
You can use
div()
method(same asdivide()
with parameteraxis
set as 0 to perform along row element-wise division:df.div(df['0'], axis = 0) #interval 0 48 #field well #Field A 0 1.0 0.500000 # 1 1.0 0.200000 # 2 1.0 0.100000 #Field B 0 1.0 0.500000 # 1 1.0 0.333333 # 2 1.0 0.166667
相关问答
更多-
@cᴏʟᴅsᴘᴇᴇᴅ改进了我的答案,所以我会在这里留下一个较慢的替代品...... import numpy as np df = pd.DataFrame(np.random.randn(4,3), index=[list('aabb'), [n + 'X' for n in list('abcd')]]) 这是使用reset_index的替代方法。 如果您想要替换多个列,这将适用。 诀窍是你不能在索引上使用replace ,所以你必须“把它带入”DataFrame ...
-
加入数据帧 - 一个是多索引列,另一个是没有(Join dataframes - one with multiindex columns and the other without)[2022-02-23]
这取决于你想要什么! 你是否希望df2中的列与df的第1或第2级列对齐? 你必须为df2的列添加一个级别 超级cheezy与pd.concat df.join(pd.concat([df2], axis=1, keys=['a'])) 更好的方法 df2.columns = pd.MultiIndex.from_product([['a'], df2.columns]) df.join(df2) It depends on what you want! Do you want the column f ... -
加入/合并两个Pandas数据帧并使用列作为多索引(Join/Merge two Pandas dataframes and use columns as multiindex)[2023-02-07]
让我们使用带有keys参数, swaplevel和sort_index : df1 = pd.DataFrame({'A':['A0','A1','A2'],'B':['B0','B1','B2'],'C':['C0','C1','C2']},index=pd.date_range('2017-01-01',periods=3, freq='M')) df2 = pd.DataFrame({'A':['A3','A4','A5'],'B':['B3','B4','B5'],'C':['C3','C4','C ... -
如果索引级别的名称排成一行, pandas自然会为你做这件事。 您可以重命名第二个数据帧的索引并相应地join 。 d1 = pd.DataFrame(['a', 'b', 'c', 'd']).transpose().set_index([0, 1, 2]) d2 = pd.DataFrame(['c', 'e']).transpose().set_index(0) d1.join(d2.rename_axis(2)) 3 1 0 1 2 a b c d e 更全面 ...
-
将concat或DataFrame.append与sort_index DataFrame.append使用: df = pd.concat([df_usdtbtc, df_ethbtc]).sort_index() 要么: df = df_usdtbtc.append(df_ethbtc).sort_index() df = pd.concat([df_usdtbtc, df_ethbtc]).sort_index() print (df) close dat ...
-
在Pandas中,创建一个MultiIndex的DataFrames面板(In Pandas, creating a Panel of MultiIndex'ed DataFrames)[2022-01-21]
我通过更新Pandas解决了这个问题。 我使用0.13,现在我有0.15.2。 I solved this problem by updating Pandas. I was using 0.13, and now I have 0.15.2. -
对字典值使用pd.concat ,并将keys参数设置为字典键: df = pd.concat(d.values(), keys=d.keys()) 结果输出: 0 1 2 A 0 0 1 2 1 2 2 4 B 0 1 1 1 1 2 2 2 Use pd.concat on the dictionary values, with the keys parameter set to the dictionary keys: df = pd.concat(d ...
-
建立 In [76]: df1 Out[76]: Level 0 A B Level 1 A1 A2 B1 0 -0.28667 1.852091 -0.134793 In [77]: df2 Out[77]: Level 0 A B Level 1 A1 A3 B1 0 -0.023582 - ...
-
您可以使用div()方法(与divide()相同,参数axis设置为0,以沿行元素划分执行: df.div(df['0'], axis = 0) #interval 0 48 #field well #Field A 0 1.0 0.500000 # 1 1.0 0.200000 # 2 1.0 0.100000 #Field B 0 1.0 0.500000 # 1 ...
-
根据更充分解释的问题,这是我之前的答案。 迭代文件并将它们读入pandas,解析日期并将其添加到数据set_index ,然后使用set_index创建多set_index 。 获得所有数据帧后,使用pd.concat将它们组合在一起: dataframes = [] for filename in filenames: df = pd.read_csv(filename) df["datetime"] = datetime.datetime.strptime(filename[8:18], ...