示例程序的大值(Big o value of sample program)
我正在尝试编程逻辑的最坏情况,需要一些澄清。
这是一个非常简单的程序,需要一个数字 - 2938023,每个字符乘以一个随机数并填入一个列表中。
填充列表后,我得到最大值作为结果。
from random import randint def test(A): result = [] for each in str(A): result.append(int(each)*randint(0,9)) return max(result) print test(2938023)
这次行动的最大案例是什么? 由于list-str(A)只迭代一次,我应该将其视为log(n)或
我应该将其视为n * n,因为再次迭代列表以获得最大值。 基于n的列表中有2个传递。
Im trying to big o worst case scenario for a programming logic and need some clarification.
Here is a very simple program, it takes a number - 2938023 and each char is multiplied by a random number and populated in a list.
Once the list is populated, I get the max value as my result.
from random import randint def test(A): result = [] for each in str(A): result.append(int(each)*randint(0,9)) return max(result) print test(2938023)
What is the big o worst case of this operation? As the list - str(A) is iterated only once, should I consider it to be log(n) or
Should I consider it n*n as the list is again iterated to get the max value. There is 2 pass on the list based on n.
原文:https://stackoverflow.com/questions/43928667
最满意答案
该错误确实解释了这个问题,你试图使用
[]example
,其中你的json示例是一个对象而不是数组,只需将其更改为:var ex example err := json.Unmarshal(body, &ex) if err != nil { fmt.Println(err) } fmt.Println(ex.Topics)
The error really explains the problem, you're trying to use
[]example
where your json example is an object not an array, simply change it to:var ex example err := json.Unmarshal(body, &ex) if err != nil { fmt.Println(err) } fmt.Println(ex.Topics)
相关问答
更多-
该错误确实解释了这个问题,你试图使用[]example ,其中你的json示例是一个对象而不是数组,只需将其更改为: var ex example err := json.Unmarshal(body, &ex) if err != nil { fmt.Println(err) } fmt.Println(ex.Topics) 操场 The error really explains the problem, you're trying to use []example where your j ...
-
在Go中解组对象数组(Unmarshaling array of objects in Go)[2022-06-03]
也许这是你的期望。 可以实现自定义的MarshalJSON / UnmarshalJSON。 package main import ( "encoding/json" "errors" "fmt" "log" ) type Timestamp struct { Zero []string One []float32 Two []float32 } func (t *Timestamp) UnmarshalJSON(b []byte) err ... -
Go's。(类型)在编组JSON时有什么意外的事情发生(Go's .(type) Does Something Unexpected When Unmarshaling JSON)[2022-12-06]
JSON数字是双精度浮点数,所以使用的默认类型是float64 。 您可以看到json.Unmarshal文档中列出的默认值。 JSON numbers are double precision floats, so the default type go uses is float64. You can see the defaults listed in the json.Unmarshal documentation. -
您可能想要确保您的myuuid变量在Data struct可见/导出:如“public”中所示。 相同的类型别名MyUUID (而不是myUUID ) type MyUUID uuid.UUID type Data struct { Uuid MyUUID } 从JSON和Go开始 : json包只能访问结构类型的导出字段(以大写字母开头的字段)。 正如Ainar G 评论的那样 , 风格指南还建议: 以初始字母或首字母缩略词命名的单词(例如“ URL ”或“ NATO ”)具有一致的情况。 例 ...
-
解组一个字符串化的json(Unmarshaling a stringified json)[2022-06-24]
使用json.RawMessage捕获变化的字段: type atomRequest struct { Stream string `json:"stream"` RawData json.RawMessage `json:"data"` Data map[string]interface{} `json:"-"` } 解组顶级JSON: var req atomRequest if err := json.Unmarshal( ... -
unmarshaling xml,具有相同元素的多个表示(unmarshaling xml with multiple representations for same element)[2024-03-05]
问题是有多个略有不同的映射。 我最后用不同的注释创建了不同的类。 编辑:另一种方式: http ://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html(但不是类型安全的,在标准的jaxb之外,我会离开现在按原样实施)。 The problem is that there are multiple, slightly different mappings. I ended up making different cla ... -
解组JSON对象的值(Unmarshaling values of JSON objects)[2023-08-15]
请注意,如果在API请求URL中使用&indexpageids=true参数,则结果将包含“pageids”数组,如下所示: str = ` { "query": { "pageids": [ "66984" ], "pages": { "66984": { "pageid": 66984, "ns": 0, ... -
根据您的十六进制转储判断,您正在接收gzip编码数据,因此您需要先使用compress / gzip对其进行解码。 尝试这样的事情 package main import ( "bytes" "compress/gzip" "encoding/json" "fmt" "io" "some/api" ) func main() { content := api.SomeAPI.SomeRequest() // []byte variable ...
-
您可以使用json.RawMessage而不是string ,这样json.Decode将不会尝试解码无效字符。 游乐场: http : //play.golang.org/p/fB-38KGAO0 type Model struct { N json.RawMessage `json:"name" bson:"name"` } func (m *Model) Name() string { return string(m.N) } func main() { s := "{\" ...
-
为什么在解组JSON时字符串和[]字节的处理方式不同?(Why are string and []bytes treated differently when unmarshaling JSON?)[2022-01-11]
encoding / json包的设计者决定应用程序必须在string值中提供有效的UTF-8文本,并且应用程序可以在[]byte值中放置任意字节序列。 包base64对[]byte值进行编码,以确保结果字符串是有效的UTF-8。 Marshal函数文档中描述了[]byte值的编码。 这个决定不是由Go语言的设计决定的。 string类型可以包含任意字节序列。 []byte类型可以包含有效的UTF-8文本。 设计人员可以在字段标记中使用一个标志来指示string或[]byte值应该编码以及使用哪个编码器,但 ...