Skip to content

Instantly share code, notes, and snippets.

View imphasing's full-sized avatar

I'm phasing imphasing

  • Flower Mound, TX
View GitHub Profile
String Reduction (25 Points)
Given a string consisting of a,b and c's, we can perform the following operation: Take any two adjacent distinct characters and replace it with the third character. For example, if 'a' and 'c' are adjacent, they can replaced with 'b'. What is the smallest string which can result by applying this operation repeatedly?
Input:
The first line contains the number of test cases T. T test cases follow. Each case contains the string you start with.
Output:
Output T lines, one for each test case containing the smallest length of the resultant string after applying the operations optimally.
Constraints:
(define-rewriter do
(lambda (form rename)
(let ((bindings (map (lambda (x) (cons (car x) (list (cadr x)))) (cadr form)))
(steps (map (lambda (x) (if (< 2 (length x)) (caddr x) (car x))) (cadr form)))
(condition (car (car (cddr form))))
(endresult (cadr (car (cddr form))))
(expr (cdddr form))
(loop-name (gensym)))
`(,(rename 'let) ,loop-name ,bindings
(define-macro (do init test . expr)
(let ((bindings (map (lambda (x) (cons (car x) (list (cadr x)))) init))
(steps (map (lambda (x) (if (< 2 (length x)) (caddr x) (car x))) init))
(condition (car test))
(endresult (cdr test))
(loop-name (gensym)))
`(let ,loop-name ,bindings
(if (not ,condition)
(begin ,(car expr) ,(cons loop-name steps))
(define (fib n)
(fib-iter 1 0 n))
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
(define fibs '())
this.Empty = false;
public IEnumerator<IScheminType> GetEnumerator ()
{
var c = Cdr();
if (!this.Empty)
yield return Car();
c = Cdr();
while (!c.Empty || c.quoted) {
yield return c.Car();
/*
* Copyright (c) 2011 Alex Fort
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
(define-rewriter or-mine
(lambda (expr)
(let ((a (cadr expr))
(b (caddr expr)))
`(let ((temp ,a))
(if temp
temp
,b)))))
(define thread-queue '())
(define halt #f)
(define (void) (if #f #t))
(define (current-continuation)
(call/cc
(define (nqueens n)
(define (dec-to n)
(let loop ((i n) (l '()))
(if (= i 0) l (loop (- i 1) (cons i l)))))
(define (try x y z)
(if (null? x)
(if (null? y)
1
0)