numbers

number read and output

坚强是说给别人听的谎言 提交于 2020-02-08 07:47:49
问题 Ok I'm having a little trouble I can get the program to read all the integers but now I'm confused how to get the program to read that is odd or even then add only odd and only even, giving totals for both def main(): myfile = open('numbers.txt','r') for line in myfile: print(line) myfile.close() myfile=open('numbers.txt','r') num1=int(myfile.readline()) num2=int(myfile.readline()) num3=int(myfile.readline()) num4=int(myfile.readline()) num5=int(myfile.readline()) num6=int(myfile.readline())

In an integer array with N elements , find the minimum k elements? [duplicate]

ⅰ亾dé卋堺 提交于 2020-02-07 05:23:04
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Worst-case O(n) algorithm for doing k-selection Given the following question : In an integer array with N elements , find the minimum k elements (k << N) You can assume that N is a large number. I'm thinking about a minimum heap , anyone has a better solution ? Regards 回答1: If K << N, min heap is good enough because creation of heap is O(n), and if K << N selecting first K items is at most O(N), otherwise you

LINQ之路 6:延迟执行(Deferred Execution)

廉价感情. 提交于 2020-02-07 04:19:19
LINQ中大部分查询运算符都有一个非常重要的特性:延迟执行。这意味着,他们不是在查询创建的时候执行,而是在遍历的时候执行(换句话说,当enumerator的MoveNext方法被调用时)。让我们考虑下面这个query: static void TestDeferredExecution() { var numbers = new List<int>(); numbers.Add(1); IEnumerable<int> query = numbers.Select(n => n * 10); // Build query numbers.Add(2); // Add an extra element after the query foreach (int n in query) Console.Write(n + "|"); // 10|20| } 可以看出,我们在查询创建之后添加的number也包含在查询结果中了,这是因为直到foreach语句对query进行遍历时,LINQ查询才会执行,这时,数据源numbers已经包含了我们后来添加的元素2,LINQ的这种特性就是延迟执行。除了下面两种查询运算符,所有其他的运算符都是延迟执行的: 返回单个元素或者标量值的查询运算符,如First、Count等。 下面这些转换运算符:ToArray、ToList、ToDictionary

0201 Python 入门演示

懵懂的女人 提交于 2020-02-07 00:54:31
列表 List: [] 集合 Set: {} 字典 Dict: {key: value} 数组 Numpy Arrays: array(List) Plot : Similar to Matlab 循环 Loop for element in set : 列表推导式(List Comprehension) numbers = [ int ( field ) for field in fields ] numbers sum ( numbers ) sum ( [ int ( field ) for field in line . split ( ) ] ) 文件操作File IO Kind of confused 函数 Function def 模块 Module use import to import a module 类 Class Person(object) __init__ : 初始化对象 self : 表示对象自身 网络数据 Data from Web url 来源: CSDN 作者: AoIntelligence 链接: https://blog.csdn.net/m0_37929374/article/details/104196072

命令行参数和可变参数

大憨熊 提交于 2020-02-04 23:44:03
命令行传参 往main函数传递参数 package com . huaying . www . param ; public class Damo01 { public static void main ( String [ ] args ) { for ( int i = 0 ; i < args . length ; i ++ ) { System . out . println ( "args[" + i + "]:" + args [ i ] ) ; } } } 编译 运行 可变参数 jdk1.5开始,java支持传递同类型可变参数给一个方法 在方法声明中,在指定参数类型后加一个省略号(…) 在一个方法中只能指定一个可变参数,它必须是方法的最后一个参数,任何普通参数必须在它之前声明 public class Damo02 { public static void main ( String [ ] args ) { Damo02 damo = new Damo02 ( ) ; damo . test ( 1 , 1 , 2 , 3 ) ; } public void test ( int x , int . . . i ) { System . out . println ( i [ 0 ] ) ; System . out . println ( i [ 1 ] ) ;

数据结构------------数组模仿队列(笔记)

旧城冷巷雨未停 提交于 2020-02-04 19:15:38
队列 第一个进去,第一个出来 数组模拟队列思路 队列本身是有序数列 队列的输出、输入分别是前后端处理的 package com.datastructure.day1; import java.util.Scanner; class test{ public static void main(String[] args) { Queue queue = new Queue(3); char key = ' '; Scanner scanner = new Scanner(System.in); boolean loop = true; while(loop){ System.out.println("s(show)"); System.out.println("e(exit)"); System.out.println("a(add)"); System.out.println("g(get)"); System.out.println("h(head)"); key = scanner.next().charAt(0); switch (key){ case 's':queue.showQueue();break; case 'a': int value = scanner.nextInt(); queue.addQueue(value);break; case 'g': try

Arrays.asList注意点

百般思念 提交于 2020-02-04 16:13:45
在进行数组转List的时候,经常会用到Arrays.asList();如果不注意的话,这个方法可能就会踩坑。 问题复现: public static void arrays2List(Integer[] numbers){ List<Integer> integers = Arrays.asList(numbers); integers.add(1); } 执行时会出现下面的问题: 在对Arrays.asList转换后的list进行操作的时候,只可以进行如下的操作。因为这里返回的list并不是java.util.ArrayList,而是Arrays中的静态内部类,java.util.Arrays$ArrayList。 可以操作的方法中没有.add方法,所以会出现错误。 解决: 1 类型强转:转换成java.util.ArrayList来解决 List<Integer> integers = new ArrayList<>(Arrays.asList(numbers)); 2 使用 Collections.addAll来解决(推荐使用) public static void arrays2List(Integer[] numbers){ List<Integer> objects = new ArrayList<>(); Collections.addAll(objects

Can I use an anchor ID starting with a number in HTML5?

陌路散爱 提交于 2020-02-03 09:38:43
问题 Can I use an anchor ID starting with a number in HTML5? <a id="1" class="anchor"></a> I was told this was a NO NO. But it seems to work fine in IE9, Firefox and Chrome. So, what gives? 回答1: Yes, this is perfectly valid in HTML5. 3.2.3.1 The id attribute The id attribute specifies its element's unique identifier (ID). [DOM] The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters. Note:

Java8新特性--流(Stream)

元气小坏坏 提交于 2020-02-01 14:45:00
1、简介 Java 8 是Java自Java 5(发布于2004年)之后的最重要的版本。这个版本包含语言、编译器、库、工具和JVM等方面的十多个新特性。在本文中我们一起来学习引入的一个新特性-流 2、Lambda表达式 在学习流之前,我们先来了解下java8中引入的Lambda表达式,它作为流很重要的一部分。Lambda表达式个构成如下所示: 2.1 有效的Lambda表达式 ()-> 42 (Apple a) -> { return a.getWeight() > 150; } //对于函数只有一行代码的,可以去掉大括号{}以及return关键字 (String s)-> s.length() (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()) 2.2 方法引用 当你需要使用方法引用时,目标放在分隔符::前,方法的名称放在后面。例如:Apple::getWeight就是引用了Apple类中定义的方法getWeight。请记住,不需要括号,因为你 没有实际调用这个方法。方法引用就是Lambda表达式(Apple a) -> a.getWeight的快捷写法。 Lambda及其等效方法引用的列子: 方法引用主要有三类: 指向静态方法的方法引用(例如Integer的parseInt方法,写作Integer:

查找列表的平均值

北战南征 提交于 2020-01-31 22:20:50
我必须在 Python 中找到列表的平均值。 到目前为止,这是我的代码 l = [15, 18, 2, 36, 12, 78, 5, 6, 9] print reduce ( lambda x, y: x + y, l) 我已经知道了,所以它可以将列表中的值相加,但是我不知道如何将其划分为它们? #1楼 l = [15, 18, 2, 36, 12, 78, 5, 6, 9] l = map(float,l) print '%.2f' %(sum(l)/len(l)) #2楼 统计 模块已 添加到python 3.4中 。 它具有计算平均值的功能,称为 均值 。 您提供的列表的示例为: from statistics import mean l = [15, 18, 2, 36, 12, 78, 5, 6, 9] mean(l) #3楼 除了将其强制转换为浮点数外,还可以在总和上加上0.0: def avg(l): return sum(l, 0.0) / len(l) #4楼 在Udacity的问题中,我也有类似的问题要解决。 我编码的不是内置函数: def list _mean(n): summing = float(sum(n)) count = float(len(n)) if n == []: return False return float(summing/count