Posts

Showing posts with the label List

In Python, Basic Shopping List Program

Image
Download

In Python, Tokenizing and Comparing Strings

Image
Download

In Python, Print Tokens In Reverse Order

Image
Download

In Python, list of random integers

Image
def change_list(src_list): # hold the middle 6 elements of the list dest_list = src_list[3:9] # use a slice to make a new list # Print the size print('The size of the list is now', len(dest_list)) # sort the new list in ascending order dest_list.sort() return dest_list def main(): # create an empty list my_list = [] from random import randrange # use a for loop to add 12 random integers for x in range(12): # all ranging from 50 to 100 my_list.append(randrange(50, 100)) # for loop to iterate over the list and display # all elements on one line separated by a single space. print('Here is the list of random integers...', end=' ') for x in my_list: print(x, end=' ') print() # display the 4th element print('The 4th element in the list is', my_list[3]) # the element at index 9 print('The element at index 9 is', my_list[9]) # the smallest element print('The smallest eleme...

In Cpp, Sum Of All Integers In Vector

Image
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ vector<int> intList; intList = {2, 4, 6, 8, 10, 12, 14, 16}; int result; result = accumulate(intList.begin(), intList.end(), 0); cout << "result: " << result << endl; return 0; }

In Python, Test If Given List Is Sorted Or Not

Image
def isSorted(stack): value = True prv = None if 0 < len(stack): prv = stack[0] for cur in stack: if prv < cur: value = False break return value if '__main__' == __name__: stack = [20, 20, 17, 11, 8, 8, 3, 2] print(isSorted(stack), stack) stack = [22] print(isSorted(stack), stack) stack = [] print(isSorted(stack), stack) stack = [21, 22, 20] print(isSorted(stack), stack) Download

In Python, Implementing Own Range() To Return List

Image
def myRange(start, stop = None, step = None): nums = None if None == step: step = 1 if None == stop: stop = start start = 0 if None != start and None != step: nums = [] while start < stop: nums.append(start) start += step return nums if '__main__' == __name__: print(myRange(3)) print(myRange(1, 7)) print(myRange(1, 7, 2)) print(myRange(-5, 1)) print(myRange(-5, 1, 2)) Download

In Python, Compute Average Of All Numbers In File

Image
def read_all(path): nums = None try: with open(path, 'r') as file: for line in file: if not nums: nums = [] try: nums.append(int(line.strip())) except ValueError: pass except IOError as e: print(e) return nums def average(nums): if not nums: return None total = 0 count = 0 for num in nums: total += num count += 1 return total/count if '__main__' == __name__: nums = read_all('number.dat') print(nums) print('average', average(nums)) Download

In Java, Compute Total Of ArrayList

Image
import java.util.Scanner; import java.util.ArrayList; public class Main{ public static double sum(ArrayList<Double> list){ double total = 0; for(double value: list) total += value; return total; } private static ArrayList<Double> readNumbers(final Scanner in, final int count){ ArrayList<Double> list = new ArrayList<>(); double real; while(count > list.size()){ System.out.print("Enter a number: "); try{ real = new Double(in.nextLine()); list.add(real); }catch(NumberFormatException e){ System.out.println("Error: Not a number!"); }catch(Exception e){ System.out.println("Error: Something went wrong!"); break; } } return list; } public static void main(String[]args){ ArrayList<Double> list = readNumbers(new Scanner(System.in), 5); double total = sum(list); System.out.printf("Sum of %s is: %.2f%n", list, total); } } Download

In Java, Implement List Of Inventory

Image
import java.util.List; import java.util.LinkedList; public class InventoryListImpl implements InventoryList{ private List<Inventory> list; public InventoryListImpl(){ list = new LinkedList<Inventory>(); } public int getSize(){ return list.size(); } public boolean add(Inventory item){ int size = getSize(), cmp; if(MAX_SIZE == size) return false; for(int i = 0; size > i; i ++){ cmp = list.get(i).getName().compareTo(item.getName()); if(0 == cmp){ return false; } else if(0 < cmp){ list.add(i, item); i = size; return true; } } list.add(item); return true; } public Inventory remove(Inventory item){ int size = getSize(); if(0 == size) return null; for(int i = 0; size > i; i ++){ if(0 == list.get(i).getName().compareTo(item.getName())){ return list.remove(i); } } return null; } public boolean contains(Inventory item){ int size = getSize(); if(0 == size) return false; for(int i = 0;...

In Java, Sorted ArrayList Of Names In Title Case

Image
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main{ static List<String> personNames = new ArrayList<String>(); public static void main(String args[]){ Scanner scanner = new Scanner(System.in); String inputName; while (true){ System.out.println("Enter the next name:"); inputName = scanner.nextLine().trim(); if (inputName.toLowerCase().equals("end")){ break; } else { insertionSort(titleCase(inputName)); } } System.out.println(personNames.toString()); } /** * * It converts the given sting in titleCase. * * @param name * * @return * */ public static String titleCase(String name){ final char[]all = name.toCharArray(); StringBuilder s1 = new StringBuilder(all.length); boolean caps = true; for(char c : all){ if(Character.isSpaceChar(c)){ caps = true; } else if(caps){ c = Character.toUpperCase(c); caps = false; ...

In C++, Remove Duplicates From List

Image
#include<iostream> #include<list> using namespace std; void printList(list<int> ints){ list<int>::iterator cur = ints.begin(), end = ints.end(); cout << "list: {"; while(cur != end){ cout << *cur; cur ++; if(cur != end) cout << ", "; } cout << "}" << endl; } int main(){ list<int> intList = {3, 23, 23, 43, 56, 11, 11, 23, 25}; printList(intList); intList.unique(); printList(intList); return 0; }

How to find unique words using python programming?

Image
words = list() print('Enter words:') while True: word = input().strip().lower() if 0 == len(word): break if word not in words: words.append(word) if words: print('Unique words given:') for word in words: print(word) else: print('No words given') Download

Recursive methods to even number and contains in ArrayList in Java

Image
Download code here .

HTML5 & CSS3 drag drop Circle with Promise API

Image
Download code here

Python user input space separated list of numbers and show min and max values

Image
Download code here .

Python allow user to input line and sort list of lines

Image
Download code here .