convert python to dart

Question:

I want to know how I can write the following python code in dart.

encrypt = str.maketrans({'a': '1', 'b': '2', 'c': '3', 'd':  '4', 'e': '5',
           'f': '6', 'g': '7', 'h': '8', 'i': '9', 'j': 'A',
           'k': 'B', 'l': 'C', 'm': 'D', 'n': 'E', 'o': 'F',
           'p': 'F', 'q': 'H', 'r': 'I', 's': 'J', 't': 'K',
           'u': 'L', 'v': 'M', 'w': 'N', 'x': 'O', 'y': 'P',
           'z': 'Q', ' ': 'X'})

text = input('Enter Text: ')
text_lower = text.lower()
enc_text = text_lower.translate(encrypt)
print(enc_text)
Asked By: red X

||

Answers:

From my understating, you are trying to encrypt a text and convert each letter into a new one. In dart there is no such method like maketrans, but you can use a normal key-value map for this purpose:

import 'dart:convert';
import 'dart:io';

main() {
  Map<String, String> letterMap = {
    'a': '1',
    'b': '2',
    'c': '3',
    'd': '4',
    'e': '5',
    'f': '6',
    'g': '7',
    'h': '8',
    'i': '9',
    'j': 'A',
    'k': 'B',
    'l': 'C',
    'm': 'D',
    'n': 'E',
    'o': 'F',
    'p': 'F',
    'q': 'H',
    'r': 'I',
    's': 'J',
    't': 'K',
    'u': 'L',
    'v': 'M',
    'w': 'N',
    'x': 'O',
    'y': 'P',
    'z': 'Q',
    ' ': 'X'
  };

  print('Enter Text: ');
  String text = stdin.readLineSync(encoding: utf8) ?? "";
  text = text.toLowerCase();
  
  String encodedText = "";
  for (var letter in text.split('')) encodedText += letterMap[letter] ?? "";
  print(encodedText);
}
Answered By: Leonardo Rignanese

do exist any library to convert dart to python?

Answered By: Pavel Černý
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.