Question d’entretien chez Yelp

a coding question about given a lowercase string 'ab',write a program to generate all possible lowercase and uppercase combination {'AB',‘Ab’,'aB' and 'ab'}

Réponses aux questions d'entretien

Utilisateur anonyme

18 févr. 2014

def combinations(string): """ >>> sorted(combinations('ab')) ['AB', 'Ab', 'aB', 'ab'] >>> sorted(combinations('abc')) ['ABC', 'ABc', 'AbC', 'Abc', 'aBC', 'aBc', 'abC', 'abc'] """ string = string.lower() if len(string) == 1: return [string, string.upper()] result = [] first_letter = string[0] for sub_string in combinations(string[1:]): result.append(first_letter + sub_string) result.append(first_letter.upper() + sub_string) return result

Utilisateur anonyme

8 juil. 2014

public LinkedList convert(String s) { LinkedList list = new LinkedList(); if(s.length() == 0 || s == null) return list; if(s.length() == 1) { list.add(s); list.add(s.toUpperCase()); return list; } char c = s.charAt(0); LinkedList returnList = convert(s.substring(1)); Iterator it = returnList.iterator(); String partial; while(it.hasNext()) { partial = it.next(); list.add(c + partial); list.add(c.toUpperCase() + partial); } return list; }

Utilisateur anonyme

24 juil. 2015

public static void main(String[] args){ toggle("ABc".toCharArray(), "abc".length()); } public static void toggle(char[] str, int pos){ if(pos == 0){ System.out.println(str); return; } str[pos-1] = Character.toUpperCase(str[pos-1]); toggle(str,pos-1); str[pos-1] = Character.toLowerCase(str[pos-1]); toggle(str, pos-1); return; }