J
J
Just__Nick2017-08-06 12:41:51
MySQL
Just__Nick, 2017-08-06 12:41:51

How to solve the problem with string compression method?

I am studying the JavaScript language on the Stepik platform and there is one task that caused me difficulties.
In short, you need to implement a string compression method based on repeated characters.
Output format:
Output a compressed string.
Sample Input:
aabccc
Sample Output:
a2bc3

I have already been able to implement the input and output function. Here she is:

var readline = require('readline');
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

rl.on('line', function(line){
    var s = line.split(' ');
   //* не понимаю, как это можно сделать. Помогите, пожалуйста
    console.log();
    rl.close();
});

Answer the question

In order to leave comments, you need to log in

5 answer(s)
F
FanatPHP, 2019-03-18
@FanatPHP

the program should do exactly what the programmer wants. if you need to update the date, then the query must update the date. no need to look for some workarounds to update the date without updating it.
If you do not want to pass the current date to the query, then mysql has the NOW () function.

A
Alexey Ukolov, 2019-03-18
@alexey-m-ukolov

update `table` set `datetime` = CURRENT_TIMESTAMP() where id = 1

V
Vitsliputsli, 2019-03-18
@Vitsliputsli

Use triggers - actions performed on an event. It's usually not a good idea to use triggers, because it's not obvious. But for things like time change is quite possible.

E
Eugene Pedya, 2019-03-18
@fpinger

Are you talking about this https://dev.mysql.com/doc/refman/8.0/en/timestamp-... ?

T
twobomb, 2017-08-06
@Just__Nick

What's the point of learning if someone else does it for you? So you will not learn anything, learn to solve problems yourself.

function compression(str){
  var out = "";
  var cnt = 1;
  for(var i = 0; i < str.length; i++)
  	if(str[i] == str[i+1] && i < str.length)
    	cnt++;
    else{
    	out+= str[i]+ (cnt > 1?cnt:"");
      cnt = 1;
      }
  return out;
}
function uncompression(str){
  var out = "";
  for(var i = 0; i < str.length; i++){
  	if(!isNaN(parseInt(str[i])))
    	continue;
  	if(i < str.length && !isNaN(parseInt(str[i+1])))
    	for(var j = 0; j < str[i+1];j++)
      	out+= str[i];
    else
    	out+=str[i];
    }
  return out;
}
alert(compression("aabcccabb"));//a2bc3ab2
alert(uncompression("a2bc3ab2"));//aabcccabb

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question