Find minimum number from a ID sequence
Here given code implementation process.
import java.util.Stack;
/*
Java program for
Find minimum number from a ID sequence
*/
public class Decode
{
public void minimumNumber(String sequence)
{
// Get the length of sequence
int n = sequence.length();
String result = "";
if (n > 0)
{
// Create an empty tracker
Stack < Integer > tracker = new Stack < Integer > ();
// Execute this loop through by length of sequence
for (int i = 0; i <= n; ++i)
{
// Add unique number
tracker.push(i + 1);
if (i == n || sequence.charAt(i) == 'I')
{
// When i equal to n or
// When sequence at i location is 'I'
// Then Append the tracker element into result
while (!tracker.empty())
{
result = result + tracker.peek();
// Remove top of tracker
tracker.pop();
}
}
}
}
// Display given sequence
System.out.println(" Given sequence : " + sequence);
// Display calculated result
System.out.println(" Result : " + result);
}
public static void main(String[] args)
{
Decode task = new Decode();
// Test
task.minimumNumber("DDIIIDI");
task.minimumNumber("IDD");
task.minimumNumber("IIDDI");
}
}
Output
Given sequence : DDIIIDI
Result : 32145768
Given sequence : IDD
Result : 1432
Given sequence : IIDDI
Result : 125436
// Include header file
#include <iostream>
#include <stack>
#include <string>
using namespace std;
/*
C++ program for
Find minimum number from a ID sequence
*/
class Decode
{
public: void minimumNumber(string sequence)
{
// Get the length of sequence
int n = sequence.length();
string result = "";
if (n > 0)
{
// Create and empty tracker
stack < int > tracker;
// Execute this loop through by length of sequence
for (int i = 0; i <= n; ++i)
{
// Add unique number
tracker.push(i + 1);
if (i == n || sequence[i] == 'I')
{
// When i equal to n or
// When sequence at i location is 'I'
// Then Append the tracker element into result
while (!tracker.empty())
{
result = result + to_string(tracker.top());
// Remove top of tracker
tracker.pop();
}
}
}
}
// Display given sequence
cout << " Given sequence : " << sequence << endl;
// Display calculated result
cout << " Result : " << result << endl;
}
};
int main()
{
Decode *task = new Decode();
// Test
task->minimumNumber("DDIIIDI");
task->minimumNumber("IDD");
task->minimumNumber("IIDDI");
return 0;
}
Output
Given sequence : DDIIIDI
Result : 32145768
Given sequence : IDD
Result : 1432
Given sequence : IIDDI
Result : 125436
// Include namespace system
using System;
using System.Collections.Generic;
/*
Csharp program for
Find minimum number from a ID sequence
*/
public class Decode
{
public void minimumNumber(String sequence)
{
// Get the length of sequence
int n = sequence.Length;
String result = "";
if (n > 0)
{
// Create and empty tracker
Stack < int > tracker = new Stack < int > ();
// Execute this loop through by length of sequence
for (int i = 0; i <= n; ++i)
{
// Add unique number
tracker.Push(i + 1);
if (i == n || sequence[i] == 'I')
{
// When i equal to n or
// When sequence at i location is 'I'
// Then Append the tracker element into result
while (!(tracker.Count == 0))
{
result = result + tracker.Peek();
// Remove top of tracker
tracker.Pop();
}
}
}
}
// Display given sequence
Console.WriteLine(" Given sequence : " + sequence);
// Display calculated result
Console.WriteLine(" Result : " + result);
}
public static void Main(String[] args)
{
Decode task = new Decode();
// Test
task.minimumNumber("DDIIIDI");
task.minimumNumber("IDD");
task.minimumNumber("IIDDI");
}
}
Output
Given sequence : DDIIIDI
Result : 32145768
Given sequence : IDD
Result : 1432
Given sequence : IIDDI
Result : 125436
package main
import "fmt"
import "strconv"
/*
Go program for
Find minimum number from a ID sequence
*/
func minimumNumber(sequence string) {
// Get the length of sequence
var n int = len(sequence)
var result string = ""
if n > 0 {
// Create and empty tracker
var tracker = make([]int,0)
// Execute this loop through by length of sequence
for i := 0 ; i <= n ; i++ {
// Add unique number
tracker = append(tracker, i + 1)
if i == n || sequence[i] == 'I' {
// When i equal to n or
// When sequence at i location is 'I'
// Then Append the tracker element into result
for (len(tracker) != 0) {
result = result + strconv.Itoa(tracker[len(tracker) - 1])
// Remove top of tracker
tracker = tracker[: len(tracker) - 1]
}
}
}
}
// Display given sequence
fmt.Println(" Given sequence : ", sequence)
// Display calculated result
fmt.Println(" Result : ", result)
}
func main() {
// Test
minimumNumber("DDIIIDI")
minimumNumber("IDD")
minimumNumber("IIDDI")
}
Output
Given sequence : DDIIIDI
Result : 32145768
Given sequence : IDD
Result : 1432
Given sequence : IIDDI
Result : 125436
<?php
/*
Php program for
Find minimum number from a ID sequence
*/
class Decode
{
public function minimumNumber($sequence)
{
// Get the length of sequence
$n = strlen($sequence);
$result = "";
if ($n > 0)
{
// Create and empty tracker
$tracker = array();
// Execute this loop through by length of sequence
for ($i = 0; $i <= $n; ++$i)
{
// Add unique number
array_push($tracker, $i + 1);
if ($i == $n || $sequence[$i] == 'I')
{
// When i equal to n or
// When sequence at i location is 'I'
// Then Append the tracker element into result
while (!empty($tracker))
{
$result = $result.strval(end($tracker));
// Remove top of tracker
array_pop($tracker);
}
}
}
}
// Display given sequence
echo(" Given sequence : ".$sequence."\n");
// Display calculated result
echo(" Result : ".$result."\n");
}
}
function main()
{
$task = new Decode();
// Test
$task->minimumNumber("DDIIIDI");
$task->minimumNumber("IDD");
$task->minimumNumber("IIDDI");
}
main();
Output
Given sequence : DDIIIDI
Result : 32145768
Given sequence : IDD
Result : 1432
Given sequence : IIDDI
Result : 125436
/*
Node JS program for
Find minimum number from a ID sequence
*/
class Decode
{
minimumNumber(sequence)
{
// Get the length of sequence
var n = sequence.length;
var result = "";
if (n > 0)
{
// Create and empty tracker
var tracker = [];
// Execute this loop through by length of sequence
for (var i = 0; i <= n; ++i)
{
// Add unique number
tracker.push(i + 1);
if (i == n || sequence.charAt(i) == 'I')
{
// When i equal to n or
// When sequence at i location is 'I'
// Then Append the tracker element into result
while (!(tracker.length == 0))
{
result = result + tracker[tracker.length - 1];
// Remove top of tracker
tracker.pop();
}
}
}
}
// Display given sequence
console.log(" Given sequence : " + sequence);
// Display calculated result
console.log(" Result : " + result);
}
}
function main()
{
var task = new Decode();
// Test
task.minimumNumber("DDIIIDI");
task.minimumNumber("IDD");
task.minimumNumber("IIDDI");
}
main();
Output
Given sequence : DDIIIDI
Result : 32145768
Given sequence : IDD
Result : 1432
Given sequence : IIDDI
Result : 125436
# Python 3 program for
# Find minimum number from a ID sequence
class Decode :
def minimumNumber(self, sequence) :
# Get the length of sequence
n = len(sequence)
result = ""
if (n > 0) :
# Create and empty tracker
tracker = []
i = 0
# Execute this loop through by length of sequence
while (i <= n) :
# Add unique number
tracker.append(i + 1)
if (i == n or sequence[i] == 'I') :
# When i equal to n or
# When sequence at i location is 'I'
# Then Append the tracker element into result
while (not(len(tracker) == 0)) :
result = result + str(tracker[-1])
# Remove top of tracker
tracker.pop()
i += 1
# Display given sequence
print(" Given sequence : ", sequence)
# Display calculated result
print(" Result : ", result)
def main() :
task = Decode()
# Test
task.minimumNumber("DDIIIDI")
task.minimumNumber("IDD")
task.minimumNumber("IIDDI")
if __name__ == "__main__": main()
Output
Given sequence : DDIIIDI
Result : 32145768
Given sequence : IDD
Result : 1432
Given sequence : IIDDI
Result : 125436
# Ruby program for
# Find minimum number from a ID sequence
class Decode
def minimumNumber(sequence)
# Get the length of sequence
n = sequence.length
result = ""
if (n > 0)
# Create and empty tracker
tracker = []
i = 0
# Execute this loop through by length of sequence
while (i <= n)
# Add unique number
tracker.push(i + 1)
if (i == n || sequence[i] == 'I')
# When i equal to n or
# When sequence at i location is 'I'
# Then Append the tracker element into result
while ((tracker.length != 0))
result = result + tracker.last.to_s
# Remove top of tracker
tracker.pop()
end
end
i += 1
end
end
# Display given sequence
print(" Given sequence : ", sequence, "\n")
# Display calculated result
print(" Result : ", result, "\n")
end
end
def main()
task = Decode.new()
# Test
task.minimumNumber("DDIIIDI")
task.minimumNumber("IDD")
task.minimumNumber("IIDDI")
end
main()
Output
Given sequence : DDIIIDI
Result : 32145768
Given sequence : IDD
Result : 1432
Given sequence : IIDDI
Result : 125436
import scala.collection.mutable._;
/*
Scala program for
Find minimum number from a ID sequence
*/
class Decode()
{
def minimumNumber(sequence: String): Unit = {
// Get the length of sequence
var n: Int = sequence.length();
var result: String = "";
if (n > 0)
{
// Create and empty tracker
var tracker: Stack[Int] = new Stack[Int]();
var i: Int = 0;
// Execute this loop through by length of sequence
while (i <= n)
{
// Add unique number
tracker.push(i + 1);
if (i == n || sequence.charAt(i) == 'I')
{
// When i equal to n or
// When sequence at i location is 'I'
// Then Append the tracker element into result
while (!tracker.isEmpty)
{
result = result + tracker.top.toString();
// Remove top of tracker
tracker.pop;
}
}
i += 1;
}
}
// Display given sequence
println(" Given sequence : " + sequence);
// Display calculated result
println(" Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Decode = new Decode();
// Test
task.minimumNumber("DDIIIDI");
task.minimumNumber("IDD");
task.minimumNumber("IIDDI");
}
}
Output
Given sequence : DDIIIDI
Result : 32145768
Given sequence : IDD
Result : 1432
Given sequence : IIDDI
Result : 125436
import Foundation;
/*
Swift 4 program for
Find minimum number from a ID sequence
*/
struct Stack
{
private
var items: [Int] = []
func peek()->Int
{
if (self.isEmpty()==false)
{
return items.first!
}
else
{
fatalError("This stack is empty.")
}
}
func isEmpty()->Bool
{
return items.count == 0
}
mutating func pop()
{
items.removeFirst()
}
mutating func push(_ data: Int)
{
items.insert(data, at: 0)
}
}
class Decode
{
func minimumNumber(_ seq: String)
{
let sequence = Array(seq);
// Get the length of sequence
let n: Int = sequence.count;
var result: String = "";
if (n > 0)
{
// Create and empty tracker
var tracker = Stack();
var i: Int = 0;
// Execute this loop through by length of sequence
while (i <= n)
{
// Add unique number
tracker.push(i + 1);
if (i == n || sequence[i] == "I")
{
// When i equal to n or
// When sequence at i location is 'I'
// Then Append the tracker element into result
while (!tracker.isEmpty())
{
result = result + String(tracker.peek());
// Remove top of tracker
tracker.pop();
}
}
i += 1;
}
}
// Display given sequence
print(" Given sequence : ", seq);
// Display calculated result
print(" Result : ", result);
}
}
func main()
{
let task: Decode = Decode();
// Test
task.minimumNumber("DDIIIDI");
task.minimumNumber("IDD");
task.minimumNumber("IIDDI");
}
main();
Output
Given sequence : DDIIIDI
Result : 32145768
Given sequence : IDD
Result : 1432
Given sequence : IIDDI
Result : 125436
import java.util.Stack;
/*
Kotlin program for
Find minimum number from a ID sequence
*/
class Decode
{
fun minimumNumber(sequence: String): Unit
{
// Get the length of sequence
val n: Int = sequence.length;
var result: String = "";
if (n > 0)
{
// Create and empty tracker
var tracker: Stack < Int > = Stack < Int > ();
var i: Int = 0;
// Execute this loop through by length of sequence
while (i <= n)
{
// Add unique number
tracker.push(i + 1);
if (i == n || sequence.get(i) == 'I')
{
// When i equal to n or
// When sequence at i location is 'I'
// Then Append the tracker element into result
while (!tracker.empty())
{
result = result + tracker.peek().toString();
// Remove top of tracker
tracker.pop();
}
}
i += 1;
}
}
// Display given sequence
println(" Given sequence : " + sequence);
// Display calculated result
println(" Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
val task: Decode = Decode();
// Test
task.minimumNumber("DDIIIDI");
task.minimumNumber("IDD");
task.minimumNumber("IIDDI");
}
Output
Given sequence : DDIIIDI
Result : 32145768
Given sequence : IDD
Result : 1432
Given sequence : IIDDI
Result : 125436
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment