Ad

fork dos

Code
Diff
  •  def pepito(testing):
        return testing
    • def pepito(chiqui):
    • return chiqui
    • def pepito(testing):
    • return testing
def pepito(chiqui):
    return chiqui

You are given two collections: "plates" and "dogs". The array "plates" contains the number of servings there are in each plate, and the array "dogs" represents the position where the dogs start from.

The dogs will go to the closest plate to them, and will eat a serving of food until there's nothing left on theri plate. Each dog, after they'vs finished, will go to he next plate for more food. How do you determine which plate they're going to? here's the criteria:

  • The'll go to the closest plate to them.
  • If two plates are at the same distance from the dog, they'll go to plate farthest from the other dogs.
  • They could eat any amount of dogs on the same plate, if theres enough food for all the dogs. If theres not enough food for all the dogs they go to the closest bowl.
  • If theres no food left, the dogs stays where they are.

Notes:

  • At most there can only be 6 servings of food per bowl
  • The two colections they always gona be the same lenght
def feedTheDogs(food,dogs):
    return dogsPosition

You will be given a string that contains space-separated words. Your task is to group the words in this way:

  • Group them by their beginning.
  • Count how many letters they share in said group.
  • Return the grouped words, ordered in ascending order, ONLY if their group size (or array length) is equal to the number of shared letters in each beginning.

See the following example:

"a abc abcd abe hi hello" -> ["a", "abc", "abcd", "abe"], ["abc", "abcd", "ab"], ["abc", "abcd"], ["abcd"], ["hi", "hello"]

See how we treat these arrays:

["a", "abc", "abcd", "abe"] -> The size of this array is 4, yet they only share in the beginning "a". (1 letter) they don't match.

["abc", "abcd", "ab"] -> The size of this array is 3, and they only share in the beginning "ab" (2 letters) they don't match.

["abc", "abcd"] -> The size of this array is 2, only share in the beginning "abc" (3 letters), they don't match.

["abcd"]-> The size of this array is 1, and they share in the beginning "abcd" (4 letters), they don't match.

["hi", "hello"]-> The size of this array is 2, and they share in the beginning "h" (1 letter), they don't match.

NOTES:

  • If 2 or more of the same word appear in the string, you should treat them as different words: Example: "is is" -> ["is", "is"] -> The size of the array is 2, and both share the beginning "is" (2 letters) -> Should add it to the returned string.
  • If in the String any word has an UpperCase Char, you should transform the String into LowerCase.Example: "Or or" -> ["or, "or"]
  • Some answers may time out so efficiency is key!
Code
Diff
  • import re
    def letter(str):
        words = re.sub(r"[^a-z]", "  ", str.lower()).split("  ")
        lst = []
        l1 = []
        l2 = []
        sol = []
        for x in words:
            word = ""
            for y in x:
                word += y
                lst.append(word)
    
        for x in sorted(set(lst)):
            for y in words:
                if y.startswith(x):
                    l1.append(y)
            l2.append(list(l1))
            l1.clear()
    
        dic = dict(zip(sorted(set(lst)), l2))
    
        for k, v in dic.items():
            if len(k) == len(v):
                for x in v:
                    sol.append(x)
    
        return sorted(sol)
    • def letras(str):
    • import re
    • import re
    • def letter(str):
    • words = re.sub(r"[^a-z]", " ", str.lower()).split(" ")
    • lst = []
    • l1 = []
    • l2 = []
    • sol = []
    • for x in words:
    • word = ""
    • for y in x:
    • word += y
    • lst.append(word)
    • for x in sorted(set(lst)):
    • for y in words:
    • if y.startswith(x):
    • l1.append(y)
    • l2.append(list(l1))
    • l1.clear()
    • dic = dict(zip(sorted(set(lst)), l2))
    • for k, v in dic.items():
    • if len(k) == len(v):
    • for x in v:
    • sol.append(x)
    • print(sol)
    • return sol
    • return sorted(sol)

You will be given a string that contains space-separated words. Your task is to group the words in this way:

  • Group them by their beginning.
  • Count how many letters they share in said group.
  • Return the grouped words, ordered in ascending order, ONLY if their group size (or array length) is equal to the number of shared letters in each beginning.

See the following example:

"a abc abcd abe hi hello" -> ["a", "abc", "abcd", "abe"], ["abc", "abcd", "ab"], ["abc", "abcd"], ["abcd"], ["hi", "hello"]

See how we treat these arrays:

["a", "abc", "abcd", "abe"] -> The size of this array is 4, yet they only share in the beginning "a". (1 letter) they don't match.

["abc", "abcd", "ab"] -> The size of this array is 3, and they only share in the beginning "ab" (2 letters) they don't match.

["abc", "abcd"] -> The size of this array is 2, only share in the beginning "abc" (3 letters), they don't match.

["abcd"]-> The size of this array is 1, and they share in the beginning "abcd" (4 letters), they don't match.

["hi", "hello"]-> The size of this array is 2, and they share in the beginning "h" (1 letter), they don't match.

NOTES:

  • If 2 or more of the same word appear in the string, you should treat them as different words:
    Example: "is is" -> ["is", "is"] -> The size of the array is 2, and both share the beginning "is" (2 letters) -> Should add it to the returned string.
  • If in the String any word has an UpperCase Char, you should transform the String into LowerCase.
    Example: "Or or" -> ["or, "or"]
  • Some answers may time out so efficiency is key!
Code
Diff
  • import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.TreeMap;
    import java.util.*;
    
    public class TreeJava {
       public static String[] solution(String str) {
            str=str.toLowerCase();
            Map<String, Integer> t = new TreeMap<>();
           String[] words = str.split("\\W");
            List<String> sol = new ArrayList<>();
            if(words.length==0) {
                return words;
            }
            int max = words[0].length();
            for (String string : words) {
                if (max < string.length())
                    max = string.length();
           }
            for (int i = 1; i < max; i++) {
               for (String string : words) {
                    if (i <= string.length())
                        t.put(string.substring(0, i), i);
                }
            }
           for (Map.Entry<String, Integer> entry : t.entrySet()) {
                String key = entry.getKey();
                Integer val = entry.getValue();
                int cont = 0;
                List<String> l = new ArrayList<>();
                for (String string : words) {
                    if (string.startsWith(key)) {
                        cont++;
                        l.add(string);
                    }
                }
                if (cont == val)
                    sol.addAll(l);
           }
         
           String [] sol2 = sol.toArray(new String[sol.size()]);
            Arrays.sort(sol2);
                return sol2;
       }
    }
    • import java.util.ArrayList;
    • import java.util.Comparator;
    • import java.util.Arrays;
    • import java.util.List;
    • import java.util.Map;
    • import java.util.TreeMap;
    • import java.util.*;
    • public class TreeJava {
    • public static String[] solution(String str) {
    • Map<String, Integer> t = new TreeMap<>();
    • String[] words = str.split("[,. ]");
    • List<String> sol = new ArrayList<>();
    • int max = words[0].length();
    • for (String string : words) {
    • if (max < string.length())
    • max = string.length();
    • }
    • for (int i = 1; i < max; i++) {
    • for (String string : words) {
    • if (i <= string.length())
    • t.put(string.substring(0, i), i);
    • }
    • }
    • for (Map.Entry<String, Integer> entry : t.entrySet()) {
    • String key = entry.getKey();
    • Integer val = entry.getValue();
    • int cont = 0;
    • List<String> l = new ArrayList<>();
    • for (String string : words) {
    • if (string.startsWith(key)) {
    • cont++;
    • l.add(string);
    • }
    • }
    • if (cont == val)
    • sol.addAll(l);
    • }
    • Comparator<String> compare=new Comparator<String>() {
    • @Override
    • public int compare(String o1, String o2) {
    • if(str.indexOf(o1)<str.indexOf(o2)) return -1;
    • if(str.indexOf(o1)>str.indexOf(o2)) return 1;
    • return 0;
    • }
    • };
    • sol.sort(compare);
    • System.out.println(sol.toString());
    • return sol.toArray(new String[sol.size()]);
    • }
    • public static String[] solution(String str) {
    • str=str.toLowerCase();
    • Map<String, Integer> t = new TreeMap<>();
    • String[] words = str.split("\\W");
    • List<String> sol = new ArrayList<>();
    • if(words.length==0) {
    • return words;
    • }
    • int max = words[0].length();
    • for (String string : words) {
    • if (max < string.length())
    • max = string.length();
    • }
    • for (int i = 1; i < max; i++) {
    • for (String string : words) {
    • if (i <= string.length())
    • t.put(string.substring(0, i), i);
    • }
    • }
    • for (Map.Entry<String, Integer> entry : t.entrySet()) {
    • String key = entry.getKey();
    • Integer val = entry.getValue();
    • int cont = 0;
    • List<String> l = new ArrayList<>();
    • for (String string : words) {
    • if (string.startsWith(key)) {
    • cont++;
    • l.add(string);
    • }
    • }
    • if (cont == val)
    • sol.addAll(l);
    • }
    • String [] sol2 = sol.toArray(new String[sol.size()]);
    • Arrays.sort(sol2);
    • return sol2;
    • }
    • }

Our job is to obtain all the words where the letters at begining are the same, but only the amount of words as letters are the same.

Example:

"why my wheat disappear, they need it for the theeme party." 

['the', 'theeme', 'they', 'wheat', 'why']

In this case we have they,the,theeme wich they start with t, h, e.

The word in the String must be treated as all letters in lowercase and must count only alphametic letters, you must return the words in the colection in alphabetic order.

Then you have to return a collection of the words following the rules.

Code
Diff
  • def letras(str):
        import re
        words = re.sub(r"[^a-z]", "  ", str.lower()).split("  ")
        lst = []
        l1 = []
        l2 = []
        sol = []
    
        for x in words:
            word = ""
            for y in x:
                word += y
                lst.append(word)
    
        for x in sorted(set(lst)):
            for y in words:
                if y.startswith(x):
                    l1.append(y)
            l2.append(list(l1))
            l1.clear()
    
        dic = dict(zip(sorted(set(lst)), l2))
    
        for k, v in dic.items():
            if len(k) == len(v):
                for x in v:
                    sol.append(x)
        print(sol)
        
        return sol
    • def letras(str):
    • return ["mejorate", "hola", "ho","esto","estas","estreñido"]
    • import re
    • words = re.sub(r"[^a-z]", " ", str.lower()).split(" ")
    • lst = []
    • l1 = []
    • l2 = []
    • sol = []
    • for x in words:
    • word = ""
    • for y in x:
    • word += y
    • lst.append(word)
    • for x in sorted(set(lst)):
    • for y in words:
    • if y.startswith(x):
    • l1.append(y)
    • l2.append(list(l1))
    • l1.clear()
    • dic = dict(zip(sorted(set(lst)), l2))
    • for k, v in dic.items():
    • if len(k) == len(v):
    • for x in v:
    • sol.append(x)
    • print(sol)
    • return sol

Dado una string de palabras, almacenar todas aquellas palabras repetidas igual a la cantidad de letras iniciales con las que comiencen.

Ejemplo: "Hola, ho esto... estas estreñido, mejorate"

En el ejemplo mostrado guardaria esto, estas y estreñido por empezar las tres palabras con "e" "s" "t"
tres letras repetidas tres palabras.
Lo mismo con Hola y ho, ademas de mejorate que al ser una sola palabra que empiece con la m

def letras(str):
    return ["mejorate", "hola", "ho","esto","estas","estreñido"]