In this case personally, I would prefer to maintain clean & reusable code.
Here's a more verbose version
from datetime import datetime def count_days(since: datetime, to: datetime) -> int: return (to - since).days + 1 def days(month: int, day: int) -> int: since = datetime(day=1, month=1, year=2022) to = datetime(day=day, month=month, year=2022) return count_days(since, to)
days=lambda m,d:(__import__("datetime").datetime(2022,m,d)-__import__("datetime").datetime(2022,1,1)).days + 1- from datetime import datetime
- def count_days(since: datetime, to: datetime) -> int:
- return (to - since).days + 1
- def days(month: int, day: int) -> int:
- since = datetime(day=1, month=1, year=2022)
- to = datetime(day=day, month=month, year=2022)
- return count_days(since, to)
Strings
Fundamentals
A little bit cleaner
import java.util.Map; import java.util.HashMap; public class phoneWords{ final static Map<Character, String> map = new HashMap<Character, String>() {{ put('0', " "); put('2', "abc"); put('3', "def"); put('4', "ghi"); put('5', "jkl"); put('6', "mno"); put('7', "pqrs"); put('8', "tuv"); put('9', "wxyz"); }}; public static String phoneWords(String str){ StringBuilder mensaje = new StringBuilder(); for (int i = 0; i < str.length(); ++i) { char letter = str.charAt(i); int cont = 0; int comparator = "79".indexOf(letter) == -1? 2: 3; while (cont != comparator && i != str.length() - 1 && str.charAt(i) == str.charAt(i + 1)) { ++cont; ++i; } mensaje.append(map.get(letter).charAt(cont)); } return mensaje.toString(); } }
import java.util.ArrayList;import java.util.List;- import java.util.Map;
- import java.util.HashMap;
- public class phoneWords{
//good luck- final static Map<Character, String> map = new HashMap<Character, String>() {{
- put('0', " ");
- put('2', "abc");
- put('3', "def");
- put('4', "ghi");
- put('5', "jkl");
- put('6', "mno");
- put('7', "pqrs");
- put('8', "tuv");
- put('9', "wxyz");
- }};
- public static String phoneWords(String str){
char letra;- StringBuilder mensaje = new StringBuilder();
List<String> arrDos = new ArrayList<String>();arrDos.add("a");arrDos.add("b");arrDos.add("c");List<String> arrTres = new ArrayList<String>();arrTres.add("d");arrTres.add("e");arrTres.add("f");List<String> arrCuatro = new ArrayList<String>();arrCuatro.add("g");arrCuatro.add("h");arrCuatro.add("i");List<String> arrCinco = new ArrayList<String>();arrCinco.add("j");arrCinco.add("k");arrCinco.add("l");List<String> arrSeis = new ArrayList<String>();arrSeis.add("m");arrSeis.add("n");arrSeis.add("o");List<String> arrSiete = new ArrayList<String>();arrSiete.add("p");arrSiete.add("q");arrSiete.add("r");arrSiete.add("s");List<String> arrOcho = new ArrayList<String>();arrOcho.add("t");arrOcho.add("u");arrOcho.add("v");List<String> arrNueve = new ArrayList<String>();arrNueve.add("w");arrNueve.add("x");arrNueve.add("y");arrNueve.add("z");if(str.isBlank()) return "";for (int i = 0; i < str.length(); i++) {- for (int i = 0; i < str.length(); ++i) {
- char letter = str.charAt(i);
- int cont = 0;
letra = str.charAt(i);if(letra != '7' && letra != '9') {while (i != str.length() - 1 && str.charAt(i) == str.charAt(i + 1) && cont != 2) {cont++;i++;}}else {while (i != str.length() - 1 && str.charAt(i) == str.charAt(i + 1) && cont != 3) {cont++;i++;}}- int comparator = "79".indexOf(letter) == -1? 2: 3;
- while (cont != comparator && i != str.length() - 1 && str.charAt(i) == str.charAt(i + 1)) {
- ++cont;
- ++i;
- }
switch (letra) {case '0':mensaje.append(" ");break;case '2':mensaje.append(arrDos.get(cont));break;case '3':mensaje.append(arrTres.get(cont));break;case '4':mensaje.append(arrCuatro.get(cont));break;case '5':mensaje.append(arrCinco.get(cont));break;case '6':mensaje.append(arrSeis.get(cont));break;case '7':mensaje.append(arrSiete.get(cont));break;case '8':mensaje.append(arrOcho.get(cont));break;case '9':mensaje.append(arrNueve.get(cont));break;}- mensaje.append(map.get(letter).charAt(cont));
- }
- return mensaje.toString();
- }
- }