Posts

Showing posts with the label sorting

Read all words from a given text file and print a count for each

Image
Clash Royale CLAN TAG #URR8PPP Read all words from a given text file and print a count for each Test.txt contains the following sentence(How much wood would a woodchuck chuck if a woodchuck could chuck wood.) This program is supposed to read all words from a given text file (until eof) and print out a count for each word. The word should be processed case-insensitive (all capitals), punctuation should be removed and the output should be sorted by frequency. However I've come to a simple problem where it's counting lines and not the words, help a brother out. Make a translation table for getting rid of non-word characters dropChars = "!@#$%ˆ& ()_+-={}|\:;"’<>,.?/1234567890" dropDict = dict([(c, '') for c in dropChars]) dropTable = str.maketrans(dropDict) Read a file and build the table. f = open("Test.txt") testList=list() lineNum = 0 table = {} # dictionary: words -> set of line numbers for line in f: testList.append(line) for ...

Std Sort make std::vector invalid

Image
Clash Royale CLAN TAG #URR8PPP Std Sort make std::vector invalid I found a bug in std::sort and in some implementations of QuickSort in particular, I do not know whether the problem is in the algorithm in general. Essence: When the elements are less than 16 all the norms, because std::sort uses an insertion sort. When there are 17 or more elements, then quick sort is used with a restriction on the depth of recursion from the logarithm of the number of elements, but vector has time to deteriorate at the first __introsort_loop iteration. There is a vector spoilage when many identical elements. Corruption happened by replacement of valid iterators with invalid iterators. Other containers may break too, I did not check. An example for simplicity with a vector of type "int", for more complex objects - crash at the time of sorting, because the invalid object is passed to the comparison function: #include <iostream> #include <vector> #include <algorithm> void quick...

Sort array by committee and then by user role

Image
Clash Royale CLAN TAG #URR8PPP Sort array by committee and then by user role I have an array that contains data similar to this: var committees = [ { committee: "Apple Com", userRole: "Member", membersNum: 11 }, { committee: "Orange Com", userRole: "Member", membersNum: 9 }, { committee: "Banana Com", userRole: "", membersNum: 13 }, { committee: "Strawberry Com", userRole: "Vice President", membersNum: 15 }, { committee: "Lemon Com", userRole: "", membersNum: 13 }, { committee: "Mango Com", userRole: "Chair", membersNum: 11 }, ] I want them sorted first, by the User Role and then by the Committee . The Committee field will always have a value, while the User Role field sometimes will be null . Currently, what I am doing to achieve this is break the array into two ar...

find the exact positions of n numbers, if each time only 3 positions individually known are in order

Image
Clash Royale CLAN TAG #URR8PPP find the exact positions of n numbers, if each time only 3 positions individually known are in order Say if there an array of length N. We need to find the exact positions of M unique numbers in N positions. example: The array of length 19 contains 8 unique numbers was in order like 3|6|1|8|1|2|9|8|6|3|5|1|8|7|1|9|8|7|1 The ARRAY given in top we don't know. We have set with 3 numbers each like below 3|6|1 2|9|8 6|1|8 9|8|7 1|8|7 1|8|7 6|3|5 8|7|1 3|5|1 8|6|3 1|8|1 5|1|8 9|8|6 8|7|1 7|1|9 1|2|9 1|9|8 8|1|2 1|3|6 7|1|3 Can we backtrack to form the above array with exact positions by using the above set of 3 numbers each at a time. I tried multiple ways to find the logic but unluckily didn't achieve the exact array given :( Need the logic in pseudo code or program in any language NOTE:- This is a circular thing--- like a circular queue created with array Although not strictly required, it's good pra...

How to sort array after combine with specification key is name and value alphabetical in php

Image
Clash Royale CLAN TAG #URR8PPP How to sort array after combine with specification key is name and value alphabetical in php I have some problem with sort data array by alphabetical I've read about the sort in the following link PHP 5 Sorting Arrays And here I have tried some functions and not as expected, look at the following picture Click Here But my question is what if i want to sort array from second key then sort alphabetically Sample Array [0] => Array ( [id] => 46759 [nama] => Albino ) [1] => Array ( [id] => 46772 [nama] => Saputra ) [2] => Array ( [id] => 46710 [nama] => Soni Putra ) [3] => Array ( [id] => 46760 [nama] => Abian ) And i want result sort by "nama" so result of that's array like this - Abian - Albino - Saputra - Soni Putra I try using sort() or asort() with many array but the letter "A" is still some that are not sorted according to the order of the letters, is there ...