Python - 列表操作练习(Python - List Manipulation Exercise)
问题:
编写一个程序,它将搜索列表以查找第一个奇数。 如果找到奇数,则找到奇数之后的第一个偶数。 返回第一个奇数和第一个偶数之间的距离。 如果没有找到奇数,或者在奇数之后没有偶数,则返回-1。
我的代码:
def go(list1): dist = 0 odd = 0 even = 0 for i in range(0,len(list1)): if list1[i] % 2 == 1: odd = list1[i] break else: odd = list1[0] list2 = list1[list1.index(odd)+1:] for i in range(0,len(list2)): if list2[i] % 2 == 0: even = list2[i] break else: even = list2[0] return list2.index(even) + list1.index(odd) + 1 - list1.index(odd) print(go([7,1,5,3,11,5,6,7,8,9,10,12345,11])) print(go([11,9,8,7,6,5,4,3,2,1,-99,7])) print(go([10,20,30,40,5,41,31,20,11,7])) print(go([32767,70,4,5,6,7])) print(go([2,7,11,21,5,7])) print(go([7,255,11,255,100,3,2])) print(go([9,11,11,11,7,1000,3])) print(go([7,7,7,11,2,7,7,11,11,2])) print(go([2,4,6,8,8]))
我的输出:
6 2 3 1 1 4 5 4 1
期望的输出:
6 2 3 1 -1 4 5 4 -1
我究竟做错了什么? 有没有比我所做的更好的方法解决这个问题?
Problem:
Write a program that will search a list to find the first odd number. If an odd number is found, then find the first even number following the odd number. Return the distance between the first odd number and the first even number. Return -1 if no odd numbers are found or there are no even numbers following an odd number.
My Code:
def go(list1): dist = 0 odd = 0 even = 0 for i in range(0,len(list1)): if list1[i] % 2 == 1: odd = list1[i] break else: odd = list1[0] list2 = list1[list1.index(odd)+1:] for i in range(0,len(list2)): if list2[i] % 2 == 0: even = list2[i] break else: even = list2[0] return list2.index(even) + list1.index(odd) + 1 - list1.index(odd) print(go([7,1,5,3,11,5,6,7,8,9,10,12345,11])) print(go([11,9,8,7,6,5,4,3,2,1,-99,7])) print(go([10,20,30,40,5,41,31,20,11,7])) print(go([32767,70,4,5,6,7])) print(go([2,7,11,21,5,7])) print(go([7,255,11,255,100,3,2])) print(go([9,11,11,11,7,1000,3])) print(go([7,7,7,11,2,7,7,11,11,2])) print(go([2,4,6,8,8]))
My Output:
6 2 3 1 1 4 5 4 1
Desired Output:
6 2 3 1 -1 4 5 4 -1
What am I doing wrong? Is there a better approach to this problem than what I have done?
原文:https://stackoverflow.com/questions/50218908
最满意答案
怎么样:
(def l (list "Name" "Id" "Type")) (defstruct location :name :id :type :visited) (assoc (apply struct location l) :visited true)
How about:
(def l (list "Name" "Id" "Type")) (defstruct location :name :id :type :visited) (assoc (apply struct location l) :visited true)
相关问答
更多-
是否有反向映射功能?(Is there a reverse-map function?)[2023-03-15]
有一些类似的juxt 。 它需要许多函数,并返回一个将参数传递给每个函数并返回一个返回值向量的函数。 所以: > ((apply juxt [inc dec str]) 1) [2 0 "1"] 主要区别在于它创建了一个向量,这当然是渴望的(即不懒惰)。原始map创建了一个懒惰的序列。 juxt也适用于具有多于1个参数的函数: > ((apply juxt [* / -]) 6 2) [12 3 4] There's juxt which is a bit similar. It takes a num ... -
我喜欢你的reduce版本很好。 我认为这是惯例。 这是一个使用列表理解的版本。 (defn foo [m f] (into {} (for [[k v] m] [k (f v)]))) I like your reduce version just fine. I think it's idiomatic. Here's a version using list comprehension anyways. (defn foo [m f] (into {} (for [[k v] m] [k ( ...
-
clojure中的数组映射示例(Array-map example in clojure)[2023-08-08]
您可以使用assoc将值添加到地图中。 assoc需要3个参数。 第一个arg是要添加到的地图,第二个arg是键,第三个是值。 该函数返回添加了键值对的旧映射。 例: (assoc {} 1 {:name "abc"}) 回报 {1 {:name "abc"}} You can use assoc to add values to a map. assoc takes 3 args. The first arg is the map that you want to add to, 2nd arg is ... -
MS PnP Unity是否像StructMap一样扫描装配体?(Does MS PnP Unity Scan for Assemblies Like StructureMap?)[2023-06-28]
程序集匹配规则用于将拦截应用于Unity构造的类,而不是容器注册。 Unity本身没有任何基于约定的扫描功能,如StructureMap。 有一个CodePlex项目可以为Unity添加类似的功能。 http://autoregistration.codeplex.com/ The Assembly Matching Rule is used for applying interception to classes constructed by Unity and not for container reg ... -
怎么样: (def l (list "Name" "Id" "Type")) (defstruct location :name :id :type :visited) (assoc (apply struct location l) :visited true) How about: (def l (list "Name" "Id" "Type")) (defstruct location :name :id :type :visited) (assoc (apply struct l ...
-
如何将具有值的Map对象数组转换为单个Map(How to convert a Array of Map object with values to a single Map)[2022-01-12]
val arr = Array(Map("k1" -> "v1"), Map("k2" -> "v2"), Map("k3" -> "v3"), Map("k4" -> "v4")) arr.flatten.toMap val arr = Array(Map("k1" -> "v1"), Map("k2" -> "v2"), Map("k3" -> "v3"), Map("k4" -> "v4")) arr.flatten.toMap -
只需将所有内容放回地图中,然后递归地遍历内容。 (defn into-xml [coll] (let [tag (into {} coll)] (update-in tag [:content] (partial map into-xml)))) 请注意,内容仅在您访问时进行转换。 编辑: Woops,错过了弦乐部分。 这是一个工作版本: (defn into-xml [coll] (if-not (string? coll) (let [tag (into {} coll ...
-
经过多次研发,我认为在结构图4.0版中没有现成的机制可以做到这一点。 但是,我想出了一个自己的动态解决方案。 创建了一个类模板并动态创建了该类。 编译并运行代码后,将该类加载到内存中。 classTemplate.txt using Castle.DynamicProxy; using StructureMap; using System.Web; using Company1.WebApplication.App1.Meta; using Company1.WebApplication.App1.Meta ...
-
Clojure - 映射值(Clojure - map values)[2021-11-18]
是的, map? 是内置功能 (map? {:a 1}) => true (map? [1]) => false Yes, map? is the inbuilt function (map? {:a 1}) => true (map? [1]) => false -
Clojure排序映射超过值(Clojure sort map over value)[2022-10-25]
在我的Clojure 1.6.0 REPL中,问题中的代码已按值排序: user=> (into (sorted-map-by (fn [key1 key2] (compare [(get x key2) key2] [(get x key1) key1]))) x) {:bla 4, :blub 2, :Foo 2, :Bla 2, :bla/bla 1, :hello 1, :foo 1, :b ...