Beginners – N smallest elements in original order (Performance Edition) (Codewars)

I am constantly on the mentioned Kata time:

task
You'll get an array of random integers and a number n. You'll need to extract n least integers out of it, preserving the original order.

Examples

performant_smallest ([1, 2, 3, 4, 5], 3) ==   [1, 2, 3]
performant_smallest ([5, 4, 3, 2, 1], 3) ==   [3, 2, 1]
performant_smallest ([1, 2, 3, 4, 1], 3) ==   [1, 2, 1]
performant_smallest ([1, 2, 3, -4, 0], 3) ==   [1, -4, 0]
performant_smallest ([2, 1, 3, 2, 3], 3) ==   [2, 1, 2]

Remarks

  • The number n is always smaller than the array length
  • The array contains duplicates, and these must be returned in order of occurrence
    Your solution must be at least so fast as a reference, the tests passed in 12000+ ms
  • If you have failed some tests, try again. Otherwise, your solution is not efficient enough.

test Suite

Tests: 1500
Array size: 4500
Speed: [-100..100]
Number of items to return: 50-100% of the array

Note: You can not use Python 2

I wrote different programs for it:

by heapq import *
from collections import counters as counters
from copy import copy

def heap_pop (lst, n):
Heap, res, i = copy (lst), [0]* n, 0
heapify
c = count (heappop (heap) for _ in range (s))
for itm in lst:
if c.get (itm, 0)> 0:
res[i] = itm
i + = 1
c[itm] - = 1
if i == n: break
res res

def sorting (lst, n):
res, i = [0]* n, 0
c = count (sorted)[:n])
for itm in lst:
if c.get (itm, 0)> 0:
res[i] = itm
i + = 1
c[itm] - = 1
if i == n: break
res res

def n_smallest (lst, n):
Heap, res, i = copy (lst), [0]* n, 0
heapify
c = count (nsmallest (n, heap))
for itm in lst:
if c.get (itm, 0)> 0:
res[i] = itm
i + = 1
c[itm] - = 1
if i == n: break
res res

def counter_sort (lst, n):
c = count (lst)
d, x, res, sm = sorted (c), {}, [0]* n, 0
for k in d:
z = min (c[k], n-sm)
x[k] = z
sm + = z
if sm == n: break
sm = 0
for itm in lst:
if x.get (itm, 0)> 0:
res[sm] = itm
sm + = 1
x[itm] - = 1
if sm == n: break
res res

def cs2 (lst, n):
c = count (lst)
d, x, res, sm = sorted (c), {}, [0]* n, 0
for k in d:
z = min (c[k], n-sm)
x[k] = z
sm + = z
if sm == n: break
sm = 0
for itm in (itm for itm in lst, if itm in x):
if x.get (itm, 0)> 0:
res[sm] = itm
sm + = 1
x[itm] - = 1
if sm == n: break
res res

def csx (lst, n):
c, sm = {i: 0 for i in the range (-100, 101)}, 0
for itm in lst: c[itm] + = 1
d, x, res, sm = sorted (c), {}, [0]* n, 0
for k in d:
z = min (c[k], n-sm)
x[k] = z
sm + = z
if sm == n: break
sm = 0
for itm in (itm for itm in lst, if itm in x):
if x.get (itm, 0)> 0:
res[sm] = itm
sm + = 1
x[itm] - = 1
if sm == n: break
res res

I have all compared (4500 random numbers in [-100, 100], and r in the [2250, 4500]with 10000 iterations) to simulate the actual data set. I am very surprised that Counter Sort did not work, considering it was a linear time and it was just twoO (2)Iterations in it.
[![enter image description here][2]][2]
counter_sort () `was clearly the best performer, but it comes down to 1068 of the test cases before it fails.

java – Swap elements in the array if the first characters match only once per first character

This is my solution for the CodingBat problem p150113. My java skills are from 2011 and I would like to know if this is still the optimal solution for such a problem. and if my code is clean and understandable.

We say that 2 strings "match" if they are not empty and their first characters are the same. Loop back and return the specified array of non-empty strings as follows: If a string matches an earlier string in the array, swap the two strings in the array. A specific first char can only cause 1 swap. Once a char has caused a swap, its later swaps are disabled. With a card, this problem can be solved by just one array going through the array.

firstSwap (["ab", "ac"]) → ["ac", "ab"]
firstSwap (["ax", "bx", "cx", "cy", "by", "ay", "aaa", "azz"]) → ["ay", "by", "cy", "cx", "bx", "ax", "aaa", "azz"]
firstSwap (["ax", "bx", "ay", "by", "ai", "aj", "bx", "by"]) → ["ay", "by", "ax", "bx", "ai", "aj", "bx", "by"]

public string[] firstSwap (String[] Strings) {
HashMap hashMap = new HashMap <> ();
int counter = 0;
int index = 0;
for (String s: strings) {
String firstChar = sSubstrate (0,1);
if (hashMap.containsKey (firstChar)) {
// make the switch in the array
if (hashMap.get (firstChar)> -1) {
index = hashMap.get (firstChar);
String temp = strings[counter];
Streicher[counter] = Strings[index];
Streicher[index] = Temp;
// Make sure it is not replaced again:
hashMap.put (firstChar, -1);
}
} else {
hashMap.put (firstChar, counter);
}

Counter = counter + 1;
}
Return string;
}

For a sorted array of n elements and element x located in the array at position k, you will find k in O (min (logk, log (n-k))).

Given a sorted array $ A[1,ldots,n]$ and element $ x $ that's because
position $ k $, We know $ x $we do not know $ k $, Write an algorithm that finds $ k $, in the $ O ( min ( log k, log (n-k)) $ temporal complexity.

Any ideas on how to solve this?

Mobile App Development Tool – Kony

If you want to build your mobile app from scratch, there are a number of tools available as part of advanced mobile app technologies. The tools below are ideal for developing a complete app from scratch, and provide multi-platform capabilities to reach a broad audience. Echo Innovate IT has put together a list of mobile app development tools that help you create a bug-free app from scratch.

Kony

When Kony connects to a single IDE, a developer can create JavaScript apps that run on all platforms. However, this tool is a bit of pinching the bag, especially if you want to create more than one mobile app, want more than a hundred users, etc.

The Kony mobile app development tool can support a variety of things including API management, catboats, voice, customer reporting, augmented reality, reference apps and many more. While the tool itself can be installed on both Windows and Mac computer systems, the associated mobile apps are used for previewing and testing on the device on which it is to run.

,

java – Edit ArrayList elements

I have a Contact.txt file that was created from an ArrayList. I have Search, Add, Find and though they are crude, they work. I have a Main, People and PeopleUtility class.

The array is output on the screen [0] First name, last name, e-mail, mobile phone. I did this to teach Java. My question is: Is there a way to "edit" any or all elements of the array? I think I have to use the .set?

Notes, hints etc. are welcome. Finally, this must come from the user input, not already hard-coded in the app.

Cheers,
Steve

Here's a part of my code that works well, even if it's "rough":

public static void searchC () {
// Browse the ArrayList
System.out.println ("Enter only LAST NAME");
String lastName = Contact.sc.nextLine ();
Int entry = 0;
for (Person Person: ppl) {// Advanced For Loop
if (person.getLastName () contains (last name)) {
System.out.println ("[" + entry + "]"+" "+ person);
} else {
System.out.println ("not found"); // I do not like that, but it works.
}
Entry ++;
}
}
}

javascript – clone element that behaves as unique elements

I create clones with hidden div. I've created a function to display the hidden div, but the feature currently only affects the original element. How do I create a function that affects individual clones?

I think I need to change every ID in each loop, but I do not know if I'm getting that change, and I still do not know how I can pass the id of the div, which will be affected as a parameter function

JavaScript:
father = document.getElementById ("Father")
child = document.getElementById (& # 39; child & # 39;)

Function showDescription (id) {
const divHidden = document.getElementById ("idElement")
divHidden.classList.toggle (& # 39; hide & # 39;);
}

for (let i = 0; i <5; i ++) {
var newElement = child.cloneNode (true);
newElement.id = "idElement" + i;
father.appendChild (newElement);

}

HTML:

Bolsonaro ainda no hospital and hospital Com pneumonia

O Presidente Jair Bolsonaro (PSL) is the Episódio Isolado febre nesta quarta feira

I wish that by clicking on a cloned div the div is only hidden in the corresponding element

java – Shrink the array size in the stack as elements are removed

I am working on the stack implementation, where I need to shrink the array size when removing objects from the stack. Just as I increase the array size when it reaches the standard limit.

I have to implement two methods for this:

  • is big: The first new method checks whether the size of the array should be reduced. This method returns true if the number of entries in the stack is less than half the size of the array and the size of the array is greater than 20.
  • to reduceThis method creates a new array that is three-quarters the size of the current array and then copies the objects in the bag into the new array.

And after I implemented the above two methods, I have to use them in the Pop() Method, so I implemented is big and to reduce Method base on the above specification and its use in pop Method.

public class StackMethods {
private static final int LIMIT = 10;
private integer[] Stack;
private int index;

public StackMethods () {
this (LIMIT);
}

public StackMethods (int cap) {
Integer[] holder = (integer[]) new object[cap];
Stack = holder;
Index = -1;
}

@Run over
public void push (integer newEntry) {
checkCapacityLimit ();
Index ++;
stack[index] = newEntry;
}

private void checkCapacityLimit () {
if (index == stack.length - 1)
stack = Arrays.copyOf (stack, 2 * stack.length);
}

@Run over
public integer pop () {
Integer top = zero;
// so it is used?
if (isBig ()) {
to reduce();
}
if (! isEmpty ()) {
top = stack[index];
stack[index] = zero;
Index--;
}
back to top;
}

// does that look right?
private boolean isBig () {
return ((index < stack.length / 2) && (stack.length > 20));
}

// looks that good too?
shrink private void () {
stack = Arrays.copyOf (stack, 3/4 * stack.length);
}
}

Any better way to write is big or to reduce Methods here?

Automation – Can all website elements be downloaded from a CSS style sheet?

I've just been asked to check a website and see if there are ways to improve page load speed.

I've noticed that there are many duplicate CSS entries in many separate CSS files. Is there a way to copy the entire CSS into a single stylesheet and then type it into some kind of software, etc., that outputs all the elements like buttons, menu items, and the like? Allow me to use such an output as a kind of checklist for creating a more efficient CSS file.

As an added benefit, it would be ideal if such software could also identify duplicate entries. The collective CSS lines are over 150,000 in size, so it would take a while to go through this manually.

Unfortunately, the site owner never created a pattern library.